using a variable as 1st param in gsub

Discussion in 'Ruby' started by louis, Jan 12, 2008.

  1. louis

    louis Guest

    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.
    louis, Jan 12, 2008
    #1
    1. Advertising

  2. louis

    Phrogz Guest

    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 )
    Phrogz, Jan 13, 2008
    #2
    1. Advertising

  3. louis

    louis Guest

    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 )
    >
    >
    louis, Jan 13, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Geathaa
    Replies:
    2
    Views:
    690
    Geathaa
    Jul 30, 2003
  2. ectoplasm
    Replies:
    12
    Views:
    635
    Zorro
    Jul 28, 2005
  3. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    160
    Robert Klemme
    Nov 9, 2005
  4. Peter Vanderhaden

    Using a variable with gsub & sub

    Peter Vanderhaden, Nov 15, 2007, in forum: Ruby
    Replies:
    4
    Views:
    69
    Justin Collins
    Nov 16, 2007
  5. Dave
    Replies:
    5
    Views:
    651
    John Bokma
    Apr 26, 2011
Loading...

Share This Page