(#18452) Explicitly casts variables as strings before calling gsub

Casts variables as strings with to_s before doing greedy substitution with gsub.

Should avoid incidents of:

```
undefined method `gsub' for nil:NilClass (NoMethodError)
```

when a variable fails to cast itself properly as a string.