chomp

C

Corey Konrad

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
 
N

Nexos

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


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

Corey Konrad

Nexos said:
"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.
 
J

Jan Friedrich

Corey said:
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
 
R

Robert Klemme

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
 
C

Corey Konrad

Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top