+ 1
What does the ".chomp" method do?
I tried a line of code with and without it and I didn't see a difference. what is it suppose to do. This is the code I used. puts"Enter your name." name = gets.chomp puts"welcome, #{name}." The output was always the same. Does ".chomp" have any purpose if so please explain it to me. Thanks you.
8 Respostas
+ 7
ignores endline
name=gets --> name="abc\n"
name=gets.chomp --> name="abc"
in other word:
gets.chomp+"\n"=gets
gets[-1]="\n"
+ 1
If I entered "Carmelo" when it asked for input, it would store the input with a line break.
.chomp removes that line break.
+ 1
suppose you are working on a computer.
and you enter something and hit enter button.
then anything you enter after hitting the enter button will appear on the next line.
if you dont want (whatever you enter after pressing the enter button) to come in the next line u use gets.chomp
hope this help
0
oh, okay so if I was to put some text after without the .chomp method it should put that on the next line right? And vice versa with the method?
0
puts"Enter your name?"
name = gets.chomp.to_s
puts"welcome, #{name}"
0
more simple method
0
Yep, there is no end line
input without .chomp
Enter your name
welcome Tom
. <-----there is end line(dot on the botom)
and with .chomp
Enter your name
welcome Tom. <----- no end line (dot after input "name")
there is no line after input
0
.Chomp adds a new line