# Using a variable as 1st param in gsub

**URL:** <https://www.ruby-forum.com/t/using-a-variable-as-1st-param-in-gsub/125861>\
**Category:** Ruby\
**Created:** [January 13, 2008, 12:40am UTC](https://www.ruby-forum.com/t/using-a-variable-as-1st-param-in-gsub/125861 "2008-01-13T00:40:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gluis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecae2f/32.png) [@gluis](https://www.ruby-forum.com/u/gluis)\
**Post date:** [January 13, 2008, 12:40am UTC](https://www.ruby-forum.com/t/using-a-variable-as-1st-param-in-gsub/125861/1 "2008-01-13T00:40:17Z")

</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:** ![gluis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecae2f/32.png) [@gluis](https://www.ruby-forum.com/u/gluis)\
**Post date:** [January 13, 2008, 6:00am UTC](https://www.ruby-forum.com/t/using-a-variable-as-1st-param-in-gsub/125861/2 "2008-01-13T06:00:32Z")

</div>

On Jan 12, 4:39 pm, louis [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 )

---

<div class="post-metadata">

**Author:** ![gluis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecae2f/32.png) [@gluis](https://www.ruby-forum.com/u/gluis)\
**Post date:** [January 13, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/using-a-variable-as-1st-param-in-gsub/125861/3 "2008-01-13T14:19:52Z")

</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!
