+ 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.

12th Jan 2017, 12:10 AM
Darrny Hedrick
Darrny Hedrick - avatar
8 ответов
+ 7
ignores endline name=gets --> name="abc\n" name=gets.chomp --> name="abc" in other word: gets.chomp+"\n"=gets gets[-1]="\n"
12th Jan 2017, 12:57 AM
kerpoo
kerpoo - avatar
+ 1
If I entered "Carmelo" when it asked for input, it would store the input with a line break. .chomp removes that line break.
9th Feb 2017, 7:45 PM
Carmelo
+ 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
13th Feb 2017, 8:15 PM
stephen haokip
stephen haokip - avatar
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?
12th Jan 2017, 1:45 AM
Darrny Hedrick
Darrny Hedrick - avatar
0
puts"Enter your name?" name = gets.chomp.to_s puts"welcome, #{name}"
28th Jan 2017, 12:07 PM
Tomasz Andr
Tomasz Andr - avatar
0
more simple method
28th Jan 2017, 12:07 PM
Tomasz Andr
Tomasz Andr - avatar
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
9th Feb 2017, 8:19 PM
Tomasz Andr
Tomasz Andr - avatar
0
.Chomp adds a new line
22nd Feb 2017, 6:43 PM
Guillermo Iglesias
Guillermo Iglesias - avatar