Ruby gsub problem when using backreference and has

2020-03-30 02:24发布

问题:

The following code defines a hash with regular expressions (keys) and replacements (values). Then it iterates over the hash and replaces the string accordingly.

Simple string substitution works well, but when I need to compute the resut before substituting it (the case of years to days change), it does not. And it is key that the hash is defined beforehand.

What am I missing? Any help will be very appreciated.

a = "After 45 years we cannot use this thing."

hash = {
  /(\d+) years/ => "#{$1.to_f*2}" + ' days',
  /cannot/      => 'of course we CAN'  
}

hash.each {|k,v| 

  a.gsub!(k) { v }
}

puts a

Thanks!

回答1:

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)
}


回答2:

The expression "$1.to_f*2" + ' days' will be executed and stored in the hash when you create the hash, not when you access it. Since at that point $1 doesn't have a value yet, it doesn't work.

You can fix this by storing lambdas in the hash like this:

hash = {/(\d+) years/ => lambda {|_| "#{$1.to_f * 2} days"},
  /cannot/      => lambda {|_| 'of course we CAN'} }

hash.each {|k,v| 
  a.gsub!(k, &v)
}


标签: ruby regex hash