Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between gets, gets.chomp and gets.chomp!?

Tags:

ruby

What is the difference between these three: gets - it gets a line with '\n' gets.chomp - it gets a line, but removes '\n'

Is that correct? What about gets.chomp! ?

like image 276
Fengson Avatar asked Sep 08 '25 16:09

Fengson


1 Answers

gets - it gets a string with '\n' at the end ( or better to say the line separator $/ at the end) , then #chomp removes the \n ( or I would say the default value of $/),and give you a new string. But #chomp! did the same change in the receiver or the source string itself, on which you called #chomp! method.

Note : #chomp! is a bang version of #chomp.

like image 131
Arup Rakshit Avatar answered Sep 10 '25 06:09

Arup Rakshit