Forum: Ruby chomp

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
139a2a2f6b330cc59a8dc6384bbf8837?d=identicon&s=25 Corey Konrad (ckonrad)
on 2007-03-15 21:15
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
076787da0775e1600ae30ea00f804809?d=identicon&s=25 Nexos (Guest)
on 2007-03-15 22:05
(Received via mailing list)
On Mar 15, 9:15 pm, Corey Konrad <0...@hush.com> 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
139a2a2f6b330cc59a8dc6384bbf8837?d=identicon&s=25 Corey Konrad (ckonrad)
on 2007-03-15 22:26
Nexos wrote:
> On Mar 15, 9:15 pm, Corey Konrad <0...@hush.com> 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.
B317fc29d33ca8d961ca846f8183184d?d=identicon&s=25 Jan Friedrich (Guest)
on 2007-03-15 22:45
(Received via mailing list)
Corey Konrad schrieb:
>
> i just dont understand why the author would make things so convoluted
> like that.
Which book from which author? Buy another book!

regards
Jan
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-15 22:48
(Received via mailing list)
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
139a2a2f6b330cc59a8dc6384bbf8837?d=identicon&s=25 Corey Konrad (ckonrad)
on 2007-03-15 23:32
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
This topic is locked and can not be replied to.