[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Corey Konrad

3/15/2007 8:16:00 PM

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-....

5 Answers

Nexos

3/15/2007 9:05:00 PM

0

On Mar 15, 9:15 pm, Corey Konrad <0...@hush.com> 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-....


"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

Corey Konrad

3/15/2007 9:26:00 PM

0

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-....
>
>
> "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-....

Jan Friedrich

3/15/2007 9:43:00 PM

0

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

Robert Klemme

3/15/2007 9:44:00 PM

0

On 15.03.2007 22:26, Corey Konrad wrote:
> 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-....
>>
>> "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

Corey Konrad

3/15/2007 10:32:00 PM

0

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-....