tags:

views:

349

answers:

3

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!

+2  A: 

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)
}
sepp2k
No, this doesn't work. `$1` is not defined inside that block; when invoking the block form of `gsub!`, the subexpression matches are passed into the block, which in your example is simply discarding its arguments.
Brian Campbell
It works now (passed the lambdas as a block instead of invoking .call on them - this way it can access `$1` (which is available from the block passed to gsub)). And yes, I am disregarding the argument to the block on purpose. The argument to the block is the whole match, not just the part in the capture. So we don't want it, we want $1.
sepp2k
Yep, you're right. I was mis-remembering how `gsub` worked.
Brian Campbell
+2  A: 

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)
}
Brian Campbell
Note that the value of years in the block will actually be "45 years" and not just "45". Doesn't matter though, since `"45 years".to_f == 45.0`.
sepp2k
Oops. You're right; I've fixed it to use `$1` now in order to avoid confusion.
Brian Campbell
A: 

Thank you very much. You have been very helpful.

fjs6