chomp

Discussion in 'Ruby' started by Corey Konrad, Mar 15, 2007.

  1. Corey Konrad

    Corey Konrad Guest

    Hello

    I am reading a book right now on ruby on rails and the author says that
    you can only use chomp with the built variable which is $_

    i am confused because it seemed to work fine when i used it like this

    print "Please enter the temperature:"
    temp = gets
    puts "The temperature is #{temp.chomp}."

    but according to the author that isnt supposed to work, am i missing
    something or is the author incorrect?

    Thanks

    --
    Posted via http://www.ruby-forum.com/.
     
    Corey Konrad, Mar 15, 2007
    #1
    1. Advertising

  2. Corey Konrad

    Nexos Guest

    On Mar 15, 9:15 pm, Corey Konrad <> wrote:
    > Hello
    >
    > I am reading a book right now on ruby on rails and the author says that
    > you can only use chomp with the built variable which is $_
    >
    > i am confused because it seemed to work fine when i used it like this
    >
    > print "Please enter the temperature:"
    > temp = gets
    > puts "The temperature is #{temp.chomp}."
    >
    > but according to the author that isnt supposed to work, am i missing
    > something or is the author incorrect?
    >
    > Thanks
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.



    "chomp" can be used on *any* string.

    If used without a target object, the $_ variable is used.
    In your particular case, $_ is filled by the call to "gets".

    Regards,

    Frédéric Delanoy
     
    Nexos, Mar 15, 2007
    #2
    1. Advertising

  3. Corey Konrad

    Corey Konrad Guest

    Nexos wrote:
    > On Mar 15, 9:15 pm, Corey Konrad <> wrote:
    >>
    >> but according to the author that isnt supposed to work, am i missing
    >> something or is the author incorrect?
    >>
    >> Thanks
    >>
    >> --
    >> Posted viahttp://www.ruby-forum.com/.

    >
    >
    > "chomp" can be used on *any* string.
    >
    > If used without a target object, the $_ variable is used.
    > In your particular case, $_ is filled by the call to "gets".
    >
    > Regards,
    >
    > Fr�d�ric Delanoy



    The author of the book emplains that in order to make chomp work with a
    variable the way i did i would have to do it like this

    print "Please enter the temperature: "
    temp = gets
    $_ = temp
    chomp
    temp = $_
    puts "The temperature is #{temp}."

    i just dont understand why the author would make things so convoluted
    like that.


    --
    Posted via http://www.ruby-forum.com/.
     
    Corey Konrad, Mar 15, 2007
    #3
  4. Corey Konrad schrieb:
    > Nexos wrote:
    > The author of the book emplains that in order to make chomp work with a
    > variable the way i did i would have to do it like this
    >
    > print "Please enter the temperature: "
    > temp = gets
    > $_ = temp
    > chomp
    > temp = $_
    > puts "The temperature is #{temp}."
    >
    > i just dont understand why the author would make things so convoluted
    > like that.

    Which book from which author? Buy another book!

    regards
    Jan
     
    Jan Friedrich, Mar 15, 2007
    #4
  5. On 15.03.2007 22:26, Corey Konrad wrote:
    > Nexos wrote:
    >> On Mar 15, 9:15 pm, Corey Konrad <> wrote:
    >>> but according to the author that isnt supposed to work, am i missing
    >>> something or is the author incorrect?
    >>>
    >>> Thanks
    >>>
    >>> --
    >>> Posted viahttp://www.ruby-forum.com/.

    >>
    >> "chomp" can be used on *any* string.
    >>
    >> If used without a target object, the $_ variable is used.
    >> In your particular case, $_ is filled by the call to "gets".
    >>
    >> Regards,
    >>
    >> Fr�d�ric Delanoy

    >
    >
    > The author of the book emplains that in order to make chomp work with a
    > variable the way i did i would have to do it like this
    >
    > print "Please enter the temperature: "
    > temp = gets
    > $_ = temp
    > chomp
    > temp = $_
    > puts "The temperature is #{temp}."
    >
    > i just dont understand why the author would make things so convoluted
    > like that.


    I don't either. Sounds strange. Btw, you can even do

    print "Please enter the temperature: "
    puts "The temperature is #{gets.chomp}."

    Kind regards

    robert
     
    Robert Klemme, Mar 15, 2007
    #5
  6. Corey Konrad

    Corey Konrad Guest

    Robert Klemme wrote:
    > On 15.03.2007 22:26, Corey Konrad wrote:
    >>> "chomp" can be used on *any* string.

    >> variable the way i did i would have to do it like this
    >>
    >> print "Please enter the temperature: "
    >> temp = gets
    >> $_ = temp
    >> chomp
    >> temp = $_
    >> puts "The temperature is #{temp}."
    >>
    >> i just dont understand why the author would make things so convoluted
    >> like that.

    >
    > I don't either. Sounds strange. Btw, you can even do
    >
    > print "Please enter the temperature: "
    > puts "The temperature is #{gets.chomp}."
    >
    > Kind regards
    >
    > robert


    yeah i know i just wanted to make sure i wasnt missing a point the
    author was making.

    thanks

    --
    Posted via http://www.ruby-forum.com/.
     
    Corey Konrad, Mar 15, 2007
    #6
    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. Aaron Powell
    Replies:
    0
    Views:
    1,211
    Aaron Powell
    Jul 24, 2003
  2. Matt Taylor

    Question on Chomp

    Matt Taylor, Sep 29, 2003, in forum: Perl
    Replies:
    2
    Views:
    387
    Matt Taylor
    Sep 29, 2003
  3. Fernando Armenta

    How you chomp in python

    Fernando Armenta, Sep 20, 2003, in forum: Python
    Replies:
    3
    Views:
    8,578
  4. Jeremy Dillworth

    Re: How you chomp in python

    Jeremy Dillworth, Sep 20, 2003, in forum: Python
    Replies:
    0
    Views:
    969
    Jeremy Dillworth
    Sep 20, 2003
  5. martin
    Replies:
    3
    Views:
    188
    Joe Smith
    Apr 15, 2006
Loading...

Share This Page