Chomp general question


For fun I tried this exercise BOTH with and without chomp.

WITHOUT chomp adds \n to the end when it displays the last line
WITH chomp removes that line

An observation I've noticed is that if we add chomp to ONLY the last line, it will all remove the extra code, the \n

This leads me to ask the question, can we only use chomp for only the very last line of code to save ourselves typing, or is this frowned upon? Please advise any pros / cons. Thanks.


I'm not sure I understand your question.

If you use:

title = gets
rating = gets.chomp

It won't change the fact that title has the \n, even though rating doesn't.


In the example below I only added chomp to only the very last line and it removes the \n. There is not an \n anywhere, so I'm wondering if we need to chomp every line or just the last to remove the \n

print "What's your name?"
first_name = gets

print "What's your last name?"
last_name = gets

print "What's your city?"
city = gets

print "What's your state? (Use 2 letter Abbreviation)"
state = gets.chomp


Yes, there are 3 \n in this code, one in first_name, one in last_name and one in city, as this code shows:



Good to know. Thanks. When I run the exact same code on my computer it only showns 1 \n, but now that you showed me this, I'll take the extra effort to chomp all lines. Thanks.