Robert Klemme
4/10/2006 9:41:00 AM
Jason Perkins wrote:
>
> On Mar 30, 2006, at 8:53 PM, Joel VanderWerf wrote:
>
>> s = original_string.gsub(/\*count\*/, 'count of #{object.count}')
>>
>> # set object somehow
>>
>> eval "\"#{s}\""
>
> Thank you, Joel.
Um, I find this piece of code a bit weired. If you determine object
*before* doing the substitution eval isn't needed at all. Maybe there
is some piece of information about the problem missing, but this
approach is preferred IMHO:
object = ...
original_string.gsub(/\*count\*/, "count of #{object.count}")
If you can only determine the value to insert during replacement then
there's always the block form:
original_string.gsub(/\*count\*/) {|m| "count of #{object.count}"}
or if you need the position, i.e. the length of the prematch
original_string.gsub(/\*count\*/) {|m| "count of #{$`.length}"}
Kind regards
robert