String#gsub!
has two forms, one in which you pass in a string as the second argument, in which variable references like $1
and $2
are replaced by the corresponding subexpression match, and one in which you pass in a block, which is called with arguments which have the subexpression matches passed in. You are using the block form when calling gsub!
, but the string in your hash is attempting to use the form in which a string is passed in.
Furthermore, the variable interpolation in your string is occurring before the match; variable interpolation happens as soon as the string is evaluated, which is at the time your hash is being constructed, while for this to work you would need variable interpolation to happen after the subexpression replacement happens (which is never the case; variable interpolation will happen first, and the resulting string would be passed in to gsub!
for gsub!
to substitute the subexpression match for $1
, but $1
would have already been evaluated and no longer in the string, as the interpolation has already occurred).
Now, how to fix this? Well, you probably want to store your blocks directly in the hash (so that the strings won't be interpreted while constructing the hash, but instead when gsub!
invokes the block), with an argument corresponding to the match, and $1
, $2
, etc. bound to the appropriate subexpression matches. In order to turn a block into a value that can be stored and later retrieved, you need to add lambda
to it; then you can pass it in as a block again by prefixing it with &
:
hash = {
/(\d+) years/ => lambda { "#{$1.to_f*2} days" },
/cannot/ => lambda { 'of course we CAN' }
}
hash.each {|k,v|
a.gsub!(k, &v)
}