Newbie question

Hi all,

I’m playing with patterns:

irb(main):001:0> name = “Jasmine”
=> “Jasmine”
irb(main):002:0> phrase = “ja”
=> “ja”
irb(main):003:0> name.sub(/ja/i, ‘**’)
=> "smine"
irb(main):004:0> name.sub(/phrase/i, '
’)
=> “Jasmine”

So, how do I tell sub that it should use the local variable phrase
instead of the string ‘phrase’ ?

TIA,
Vamsee.

irb(main):001:0> a = “Jasmine”
=> “Jasmine”
irb(main):002:0> phrase = “jas”
=> “jas”
irb(main):003:0> a.sub(/#{phrase}/i, ‘**’)
=> “**mine”
irb(main):004:0>

Am Dienstag 22 November 2005 13:29 schrieb Vamsee K.:

irb(main):001:0> name = “Jasmine”
=> “Jasmine”
irb(main):002:0> phrase = “ja”
=> “ja”
irb(main):003:0> name.sub(/ja/i, ‘**’)
=> "smine"
irb(main):004:0> name.sub(/phrase/i, '
’)
=> “Jasmine”

Just the same way you do variable substitution in strings (like in
puts):

irb(main):005:0> name.sub(/#{phrase}/i, ‘**’)
=> “**smine”

Benedikt

ALLIANCE, n. In international politics, the union of two thieves who
have their hands so deeply inserted in each other’s pockets that
they cannot separately plunder a third.
(Ambrose Bierce, The Devil’s Dictionary)

Vamsee K. wrote:

irb(main):004:0> name.sub(/phrase/i, ‘**’)
=> “Jasmine”

So, how do I tell sub that it should use the local variable phrase
instead of the string ‘phrase’ ?

Try name.sub(/#{phrase}/i, ‘**’)

robert

Robert K. wrote:

Try name.sub(/#{phrase}/i, ‘**’)

Got it. Thanks all!

Vamsee K. wrote:

irb(main):004:0> name.sub(/phrase/i, ‘**’)
=> “Jasmine”

So, how do I tell sub that it should use the local variable phrase
instead of the string ‘phrase’ ?

irb(main):013:0> phrase = /ja/i
=> /ja/i
irb(main):014:0> “Jasmine”.sub(phrase, ‘**’)
=> “**smine”