Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Operators and Control Structures Ruby Control Structures The Ruby Else Statement

Brandon Jacobi
Brandon Jacobi
4,702 Points

I'm a little confused on the .chomp method?

I don't really get what .chomp means. I looked it up and it still doesn't help.

print "Enter name: "
name = gets.chomp

if name == "Jason" 
  puts "That's not my name."
else
  puts "Hi #{name}!"
end

print "Would you like to see your name backwords? (yes or no)"
answer = gets.chomp

So what does .chomp mean?

brian biggers
brian biggers
8,556 Points

Was this really mentioned in Ruby basics? I just did it and I don't remember this being covered.

2 Answers

Good question! .chomp deletes any whitespace (newlines, tabs, spaces, etc.) at the end of a string only is there is whitespace at the end of the string. If you just used "gets" by itself and inputted "a" then pressed enter, it will return "a\n"! The reason it returns your input followed by a newline is because you type in your input, then press "Enter", which creates an extra newline. That's why most of the time we .chomp the gets so that Ruby will get rid of that extra whitespace. :smile:

Hope it helps and Happy coding! :) ~Alex

=]