Trying to use gsub using variables

From: jackster the jackle <contact@thirdorder.net>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Saturday, September 27, 2008 2:22:55 PM
Subject: Re: Trying to use gsub using variables

thanks guys...I'm making progress but what I have been slowly leading up
to is searching using a variable then replacing it with a variable.

acl = "now is the time"
x = time
y = TEST

you meant, didn't you?

x = "time"
y = 'TEST'

acl.gsub(/x/,y)

How do I get gsub to use the value of x rather "x" itself in my match?

You can use the #{ruby code} notation that evaluates the ruby code and inserts
the value like in double quoted strings.
In your case that would be something like:

acl.gsub(/#{x}/,y)

Though the content of x should not be interpreted, you should write:

acl.gsub(/#{Regexp.escape(x)}/,y)

thanks

you are most welcome,

Christophe

ยทยทยท

----- Original Message ----

Jackster
--
Posted via http://www.ruby-forum.com/\.