Forum: Ruby newbie question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
vamlists (Guest)
on 2005-11-22 14:32
(Received via mailing list)
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.
m.fellinger (Guest)
on 2005-11-22 14:36
(Received via mailing list)
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.:
ruby (Guest)
on 2005-11-22 14:36
(Received via mailing list)
> 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)
bob.news (Guest)
on 2005-11-22 14:40
(Received via mailing list)
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
vamlists (Guest)
on 2005-11-22 15:01
(Received via mailing list)
Robert K. wrote:

>Try name.sub(/#{phrase}/i, '**')
>
>
Got it. Thanks all!
w_a_x_man (Guest)
on 2005-11-22 16:25
(Received via mailing list)
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"
This topic is locked and can not be replied to.