# Using a variable as 1st param in gsub

**URL:** <https://rubytalk.org/t/using-a-variable-as-1st-param-in-gsub/43727>\
**Category:** ruby-talk\
**Created:** [12 January 2008 23:39 UTC](https://rubytalk.org/t/using-a-variable-as-1st-param-in-gsub/43727 "2008-01-12T23:39:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![louis](https://avatars.discourse-cdn.com/v4/letter/l/ecb155/32.png) [@louis](https://rubytalk.org/u/louis)\
**Post date:** [12 January 2008 23:39 UTC](https://rubytalk.org/t/using-a-variable-as-1st-param-in-gsub/43727/1 "2008-01-12T23:39:43Z")

</div>

hi all,

i'm trying to use a user entered variable in a gsub method and for  
some reason, if i insert it within a regex it doesn't work. passing it  
as-is works fine. en example will be clearer:

> **···**
>
> #  
> #  
> user\_variable = 'some\_word' # retrieved from 'gets.chomp'  
> cleaned\_variable = '^' + user\_variable + '|\s' + user\_variable + '(?!\w)'
> 
> # print out variable to see if concat worked:  
> puts cleaned\_variable #=\> prints out correct regex
> 
> p some-sentence-string.gsub(%r{cleaned\_variable},'TEST') # =\> prints  
> out sentence unchanged
> 
> #---  
> # however if i pass it like this, it works:
> 
> p some-sentence-string.gsub(%r{user\_variable},'TEST')  
> OR  
> p some-sentence-string.gsub(user\_variable,'TEST')
> 
> #----
> 
> now the problem is if that variable exists within another word (e.g.  
> 'he' matches 'hello', 'the', 'she', etc.), those match too. here's the  
> clincher: in irb it works as i expect. only when run inside a script  
> does it not work! that is, the exact same code, pasted in irb, runs  
> correctly.
> 
> is there something different about the parser? i thought irb just used  
> the same parser. what am i doing wrong? any tips for troubleshooting?  
> is this a bug? am i a bug?
> 
> thanks,
> 
> louis
> 
> p.s. i've also  
> 1. concatenated '/' in the cleaned variable  
> 2. tried to pass w/o %r{} using /cleaned\_variable/ and /user\_variable/  
> (i know, i didn't expect the latter to do anything different--just  
> trying anything)
> 
> p.p.s. using similar methods in python works fine (for whatever that's  
> worth). i haven't tried it in another language yet.
> 
> p. \* 3 + s. sorry if this is a repost--i first used nabble but that  
> post didn't show up on any other mirror of the mailing-list, so i'm  
> posting again.

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [13 January 2008 04:59 UTC](https://rubytalk.org/t/using-a-variable-as-1st-param-in-gsub/43727/2 "2008-01-13T04:59:59Z")

</div>

%r{foo} is the same as /foo/...you're using the name of the variable,  
not the contents. You want:  
/#{foo}/ or %r{#{foo}} or %r|#{foo}| or ...  
or Regexp.new( foo )

> **···**
>
> On Jan 12, 4:39 pm, louis \<louisju...@gmail.com\> wrote:
> 
> > user\_variable = 'some\_word' # retrieved from 'gets.chomp'  
> > cleaned\_variable = '^' + user\_variable + '|\s' + user\_variable + '(?!\w)'
> > 
> > # print out variable to see if concat worked:  
> > puts cleaned\_variable #=\> prints out correct regex
> > 
> > p some-sentence-string.gsub(%r{cleaned\_variable},'TEST') # =\> prints  
> > out sentence unchanged

---

<div class="post-metadata">

**Author:** ![louis](https://avatars.discourse-cdn.com/v4/letter/l/ecb155/32.png) [@louis](https://rubytalk.org/u/louis)\
**Post date:** [13 January 2008 13:19 UTC](https://rubytalk.org/t/using-a-variable-as-1st-param-in-gsub/43727/3 "2008-01-13T13:19:18Z")

</div>

ahah! makes perfect sense. I thought interpolation in ruby using that  
construct was for double quoted strings. and since it worked in irb I  
was confused.  
I'm not at a machine now so I can't test your suggestion but I'll  
assume its correct.

thanks a lot for the help!

> **···**
>
> On 1/12/08, Phrogz \<phrogz@mac.com\> wrote:
> 
> > On Jan 12, 4:39 pm, louis \<louisju...@gmail.com\> wrote:  
> > \> user\_variable = 'some\_word' # retrieved from 'gets.chomp'  
> > \> cleaned\_variable = '^' + user\_variable + '|\s' + user\_variable + '(?!\w)'  
> > \>  
> > \> # print out variable to see if concat worked:  
> > \> puts cleaned\_variable #=\> prints out correct regex  
> > \>  
> > \> p some-sentence-string.gsub(%r{cleaned\_variable},'TEST') # =\> prints  
> > \> out sentence unchanged
> > 
> > %r{foo} is the same as /foo/...you're using the name of the variable,  
> > not the contents. You want:  
> > /#{foo}/ or %r{#{foo}} or %r|#{foo}| or ...  
> > or Regexp.new( foo )
