How to use "gets" and "gets.chomp" in Ruby
RubyRuby Problem Overview
I learned that gets
creates a new line and asks the user to input something, and gets.chomp
does the same thing except that it does not create a new line. gets
must return an object, so you can call a method on it, right? If so, lets name that object returned by gets
as tmp
, then you can call the chomp
method of tmp
. But before gets
returns tmp
, it should print a new line on the screen. So what does chomp
do? Does it remove the new line after the gets
created it?
Another way to re-expound my question is: Are the following actions performed when I call gets.chomp
?
gets
prints a new linegets
returnstmp
tmp.chomp
removes the new line- User input
Is this the right order?
Ruby Solutions
Solution 1 - Ruby
gets
lets the user input a line and returns it as a value to your program. This value includes the trailing line break. If you then call chomp
on that value, this line break is cut off. So no, what you have there is incorrect, it should rather be:
gets
gets a line of text, including a line break at the end.- This is the user input
gets
returns that line of text as a string value.- Calling
chomp
on that value removes the line break
The fact that you see the line of text on the screen is only because you entered it there in the first place. gets
does not magically suppress output of things you entered.
Solution 2 - Ruby
The question shouldn't be "Is this the right order?" but more "is this is the right way of approaching this?"
Consider this, which is more or less what you want to achieve:
-
You assign a variable called
tmp
the return value ofgets
, which is a String. -
Then you call String's
chomp
method on that object and you can see thatchomp
removed the trailing new-line.Actually what
chomp
does, is remove the Enter character ("\n
") at the end of your string. When you type h e l l o, one character at a time, and then press Entergets
takes all the letters and the Enter key's new-line character ("\n
").1. tmp = gets hello =>"hello\n" 2. tmp.chomp "hello"
gets
is your user's input. Also, it's good to know that *gets
means "get string" and puts
means "put string". That means these methods are dealing with Strings only.
Solution 3 - Ruby
chomp is the method to remove trailing new line character i.e. '\n' from the the string. whenever "gets" is use to take i/p from user it appends new line character i.e.'\n' in the end of the string.So to remove '\n' from the string 'chomp' is used.
str = "Hello ruby\n"
str = str.chomp
puts str
o/p
"Hello ruby"
Solution 4 - Ruby
chomp
returns a new String with the given record separator removed from the end of str
(if present).
See the Ruby String API for more information.
Solution 5 - Ruby
-
"gets" allows user input but a new line will be added after the string (string means text or a sequence of characters)
"gets.chomp" allows user input as well just like "gets", but there is not going to be a new line that is added after the string.
Proof that there are differences between them:
Gets.chomp
puts "Enter first text:"
text1 = gets.chomp
puts "Enter second text:"
text2 = gets.chomp
puts text1 + text2
Gets:
puts "Enter first text:"
text1 = gets
puts "Enter second text:"
text2 = gets
puts text1 + text2
Copy paste the code I gave you, run and you will see and know that they are both different.
Solution 6 - Ruby
For example:
x = gets
y = gets
puts x+y
and
x = gets.chomp
y = gets.chomp
puts x+y
Now run the two examples separately and see the difference.