From: louis Date: 2008-01-13T22:19:18+09:00 Subject: Re: using a variable as 1st param in gsub 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 wrote: > On Jan 12, 4:39 pm, louis 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 ) > >