newbie question

V

Vamsee Kanakala

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

Michael Fellinger

--nextPart1151914.iA3RVV388T
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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 Kanakala:
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.

--nextPart1151914.iA3RVV388T
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDgxBPMdQeL6eBxhIRAkPIAJ9Gkt95Sn4Mr6IfJhA1x9yYNYzJzwCdEC48
/4hGvSnSXnCt2weZviu0jhY=
=eTGv
-----END PGP SIGNATURE-----

--nextPart1151914.iA3RVV388T--
 
B

Benedikt Heinen

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)
 
R

Robert Klemme

Vamsee said:
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' ?

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

robert
 
W

William James

Vamsee said:
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' ?


irb(main):013:0> phrase = /ja/i
=> /ja/i
irb(main):014:0> "Jasmine".sub(phrase, '**')
=> "**smine"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,625
Members
45,322
Latest member
ClaritaMcI

Latest Threads

Top