Why does Ruby’s ‘gets’ includes the closing newline?

Like puts (which sounds similar), it is designed to work with lines, using the \n character.

gets takes an optional argument that is used for “splitting” the input (or “just reading till it arrives). It defaults to the special global variable $/, which contains a \n by default.

gets is a pretty generic method for readings streams and includes this separator. If it would not do it, parts of the stream content would be lost.

Leave a Comment