# Trying to use gsub using variables

**URL:** <https://rubytalk.org/t/trying-to-use-gsub-using-variables/49324>\
**Category:** ruby-talk\
**Created:** [27 September 2008 12:55 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49324 "2008-09-27T12:55:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maestroiut\_Rubytalk](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Maestroiut\_Rubytalk](https://rubytalk.org/u/Maestroiut_Rubytalk)\
**Post date:** [27 September 2008 12:55 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49324/1 "2008-09-27T12:55:52Z")

</div>

> 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/\](http://www.ruby-forum.com/%5C).
