Short answer
Use dvar = "$" + name.tr(".?\/ !@ \#{}$%^&*()``~", '')
Long answer
The problem you are facing is that gsub! the call returns zero. You cannot concatenate (+) a string with zero.
This is because you have the wrong Regexp. You do not avoid special regular expression characters such as $, * and., Just for starters. Also, as of now, gsub will only match if your string contains all of these characters in sequence. You must use the pipe (|) operator to perform an OR type operation.
GSUB! will also return zero if no replacements have occurred.
See the gsub and gsub documentation! here: http://ruby-doc.org/core/classes/String.html#M001186
I think you should replace gsub! with gsub. Do you really need to change the name ?
Example:
name = "m$var.name$$" dvar = "$" + name.gsub!(/\$|\.|\*/, "") # $ or . or * # dvar now contains $mvarname and name is mvarname
Your line is fixed:
dvar = "$" + name.gsub(/\.|\?|\/|\!|\@|\\|\#|\{|\}|\$|\%|\^|\&|\*|\(|\)|\`|\~/, "")
As the designated J -_- L, you can also use the character class ([]), which makes it a little clearer, I think. Well, anyway, it's hard to mentally disassemble.
dvar = "$" + name.gsub(/[\.\?\/\!\@\\\#\{\}\$\%\^\&\*\(\)\`\~]/, "")
But since what you are doing is a simple character swap, the best method is tr (again reminded J -_- L!):
dvar = "$" + name.tr(".?\/ !@ \#{}$%^&*()`~", '')
Easier to read and make changes.