simple gsub method

Discussion in 'Ruby' started by Jack Smith, Oct 1, 2008.

  1. Jack Smith

    Jack Smith Guest

    I have simplified my method for searching test to the following:

    def replacer(a, b)
    x = "now is the time for all good men to come to the aid of their
    country"
    x.gsub(/#{a}/, b)
    puts x
    end

    puts "enter what to replace: "
    j = gets
    puts "enter replacer word: "
    h = gets

    replacer(j, h)

    Can someone help me determine why gsub can't match my "a" value?

    thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Jack Smith, Oct 1, 2008
    #1
    1. Advertising

  2. Alle Wednesday 01 October 2008, Jack Smith ha scritto:
    > I have simplified my method for searching test to the following:
    >
    > def replacer(a, b)
    > x = "now is the time for all good men to come to the aid of their
    > country"
    > x.gsub(/#{a}/, b)
    > puts x
    > end
    >
    > puts "enter what to replace: "
    > j = gets
    > puts "enter replacer word: "
    > h = gets
    >
    > replacer(j, h)
    >
    > Can someone help me determine why gsub can't match my "a" value?
    >
    > thanks


    Strings returned by gets end in a newline, which doesn't exist in your string,
    so no match happens. To solve this, you can do

    j = gets.chomp

    and the same for h.

    I hope this helps

    Stefano
     
    Stefano Crocco, Oct 1, 2008
    #2
    1. Advertising

  3. Jack Smith

    Jack Smith Guest

    It does help Stefano as I had forgotton about \n so thanks for that but
    my script still does not match on my "a" value even with the chomp
    method added.




    Stefano Crocco wrote:
    > Alle Wednesday 01 October 2008, Jack Smith ha scritto:
    >> j = gets
    >> puts "enter replacer word: "
    >> h = gets
    >>
    >> replacer(j, h)
    >>
    >> Can someone help me determine why gsub can't match my "a" value?
    >>
    >> thanks

    >
    > Strings returned by gets end in a newline, which doesn't exist in your
    > string,
    > so no match happens. To solve this, you can do
    >
    > j = gets.chomp
    >
    > and the same for h.
    >
    > I hope this helps
    >
    > Stefano


    --
    Posted via http://www.ruby-forum.com/.
     
    Jack Smith, Oct 1, 2008
    #3
  4. Jack Smith wrote:
    > x.gsub(/#{a}/, b)


    You don't use the return value of gsub, so it is thrown away and this line
    basically does nothing. Calling gsub on x, does not change x (gsub! would).

    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Oct 1, 2008
    #4
  5. Jack Smith

    Jack Smith Guest

    I take that back Stefano....you solved it for me!
    I forgot to add the x.gsub! ....when I did, my script is working.

    thanks a REAL lot for helping me with this!

    j


    Jack Smith wrote:
    > It does help Stefano as I had forgotton about \n so thanks for that but
    > my script still does not match on my "a" value even with the chomp
    > method added.
    >
    >
    >
    >
    > Stefano Crocco wrote:
    >> Alle Wednesday 01 October 2008, Jack Smith ha scritto:
    >>> j = gets
    >>> puts "enter replacer word: "
    >>> h = gets
    >>>
    >>> replacer(j, h)
    >>>
    >>> Can someone help me determine why gsub can't match my "a" value?
    >>>
    >>> thanks

    >>
    >> Strings returned by gets end in a newline, which doesn't exist in your
    >> string,
    >> so no match happens. To solve this, you can do
    >>
    >> j = gets.chomp
    >>
    >> and the same for h.
    >>
    >> I hope this helps
    >>
    >> Stefano


    --
    Posted via http://www.ruby-forum.com/.
     
    Jack Smith, Oct 1, 2008
    #5
    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. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    182
    Robert Klemme
    Nov 9, 2005
  2. Hoppy

    gsub method question

    Hoppy, May 10, 2006, in forum: Ruby
    Replies:
    4
    Views:
    96
    Hoppy
    May 10, 2006
  3. Dustin Anderson

    simple gsub question \' \` what?

    Dustin Anderson, Mar 30, 2007, in forum: Ruby
    Replies:
    3
    Views:
    144
    Rob Biedenharn
    Mar 30, 2007
  4. Derek Cannon

    Creating a gsub! method for Arrays

    Derek Cannon, Mar 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    207
    Justin Collins
    Mar 26, 2010
  5. Mr. Bill
    Replies:
    2
    Views:
    569
    Mr. Bill
    Dec 30, 2010
Loading...

Share This Page