Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > chomp

Reply
Thread Tools

chomp

 
 
Corey Konrad
Guest
Posts: n/a
 
      03-15-2007
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/.

 
Reply With Quote
 
 
 
 
Nexos
Guest
Posts: n/a
 
      03-15-2007
On Mar 15, 9:15 pm, Corey Konrad <(E-Mail Removed)> 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,

Frdric Delanoy

 
Reply With Quote
 
 
 
 
Corey Konrad
Guest
Posts: n/a
 
      03-15-2007
Nexos wrote:
> On Mar 15, 9:15 pm, Corey Konrad <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Jan Friedrich
Guest
Posts: n/a
 
      03-15-2007
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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-15-2007
On 15.03.2007 22:26, Corey Konrad wrote:
> Nexos wrote:
>> On Mar 15, 9:15 pm, Corey Konrad <(E-Mail Removed)> 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

 
Reply With Quote
 
Corey Konrad
Guest
Posts: n/a
 
      03-15-2007
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"chomp,chop" usage i.e. chop immediately after chomp martin Perl Misc 3 04-15-2006 08:09 PM
Question on Chomp Matt Taylor Perl 2 09-29-2003 11:54 AM
How you chomp in python Fernando Armenta Python 3 09-21-2003 10:09 AM
Re: How you chomp in python Jeremy Dillworth Python 0 09-20-2003 02:36 AM
Chomp not working properly with Sendmail Aaron Powell Perl 0 07-24-2003 02:24 PM



Advertisments