[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

turning off interpolation in a regex

Jason N.Perkins

3/31/2006 1:33:00 AM

I need to be able to find a run of characters (like *count*) in a
string and substitute in the value #{object.count} so that I'm left
with a string that can itself be interpolated. The method for doing
this that jumped out was to use the string's gsub method like this:

original_string.gsub(/\*count\*/, /count of #{object\.count}/

(Old hands at Ruby are now chuckling, and rightly so) How do I go
about getting the literal text in the substitution portion without
getting an, "NameError: undefined local variable or method `object'
for main:Object"?

Any assistance, pointers or suggestions appreciated.

--
Jason Perkins
jperkins@sneer.org

"The computer allows you to make mistakes
faster than any other invention, with the
possible exception of handguns and tequila."




3 Answers

Joel VanderWerf

3/31/2006 2:53:00 AM

0

Jason Perkins wrote:
> I need to be able to find a run of characters (like *count*) in a string
> and substitute in the value #{object.count} so that I'm left with a
> string that can itself be interpolated. The method for doing this that
> jumped out was to use the string's gsub method like this:
>
> original_string.gsub(/\*count\*/, /count of #{object\.count}/
>
> (Old hands at Ruby are now chuckling, and rightly so) How do I go about
> getting the literal text in the substitution portion without getting an,
> "NameError: undefined local variable or method `object' for main:Object"?

s = original_string.gsub(/\*count\*/, 'count of #{object.count}')

# set object somehow

eval "\"#{s}\""

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Jason N.Perkins

3/31/2006 3:07:00 AM

0


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.

--
Jason Perkins
jperkins@sneer.org

"The computer allows you to make mistakes
faster than any other invention, with the
possible exception of handguns and tequila."




Robert Klemme

4/10/2006 9:41:00 AM

0

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