Alex Rivera | Logout

Why does Ruby's 'gets' includes the closing newline?

Asked 2011-06-21T21:58:44.370
21

I never need the ending newline I get from gets. Half of the time I forget to chomp it and it is a pain in the....

Why is it there?

Edit
Report

1 Answer

0

From a performance perspective, the better question would be "why should I get rid of it?". It's not a big cost, but under the hood you have to pay to chomp the string being returned. While you may never have had a case where you need it, you've surely had plenty of cases where you don't care -- gets s; puts stuff() if s =~ /y/i, etc. In those cases, you'll see a (tiny, tiny) performance improvement by not chomping.

answered 2011-06-21T22:09:37.193

Your Answer