# Chomp

**URL:** <https://www.ruby-forum.com/t/chomp/88951>\
**Category:** Ruby\
**Created:** [March 15, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/chomp/88951 "2007-03-15T21:15:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 15, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/chomp/88951/1 "2007-03-15T21:15:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 15, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/chomp/88951/2 "2007-03-15T22:05:37Z")

</div>

On Mar 15, 9:15 pm, Corey K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 15, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/chomp/88951/3 "2007-03-15T22:26:23Z")

</div>

Nexos wrote:

> On Mar 15, 9:15 pm, Corey K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 15, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/chomp/88951/4 "2007-03-15T22:48:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 15, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/chomp/88951/5 "2007-03-15T23:32:11Z")

</div>

Robert K. wrote:

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

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 15, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/chomp/88951/6 "2007-03-15T22:45:44Z")

</div>

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