Fengson
Fengson

Reputation: 4912

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

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! ?

Upvotes: 4

Views: 2448

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

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.

Upvotes: 7

Related Questions