20

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?

5
  • 2
    woodworking saying: you can always cut more wood from a board, but you can't put it back.
    – DGM
    Jun 22, 2011 at 0:19
  • 4
    @DGM You can always add "\n".
    – sawa
    Jun 22, 2011 at 0:43
  • @sawa: If you're sure there was one there to start with. Jun 22, 2011 at 7:16
  • @Andrew The only case when you don't get one is when you reached EOF. But will that matter?
    – sawa
    Jun 22, 2011 at 18:29
  • 1
    gets is a standard method in many languages, rooted all the way back to C. Its behavior is the same as C's, which is probably for consistency. I doubt it's going to change, so you probably want to get used to either automatically adding chomp, or pay more attention to the data-type you need and add chomp only when necessary. It's just one of those chores in programming that we learn to do. Dec 29, 2013 at 5:27

5 Answers 5

7

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.

4
var = gets.chomp 

This puts it all on one line for you.

1
  • 6
    This answer does not address the question at all. I wonder why it got upvoted so much. Sep 11, 2015 at 7:11
2

If you look at the documentation of IO#gets, you'll notice that the method takes an optional parameter sep which defaults to $/ (the input record separator). You can decide to split input on other things than newlines, e.g. paragraphs ("a zero-length separator reads the input a paragraph at a time (two successive newlines in the input separate paragraphs)"):

>> gets('')
dsfasdf
fasfds


dsafadsf    #=> "dsfasdf\nfasfds\n\n"
2
  • 2
    Still, that begs the question of why it should capture and return the separator -- "1,2,3".split(',') doesn't return the commas. I'd argue (as I did in my answer) that the reason for the discrepancy is that it's no extra "work" to leave the commas out of the result for split, but gets has to buffer the record separator before it can notice that it's come along.
    – Coderer
    Jun 21, 2011 at 22:35
  • Why should it strip the separator? IO#gets is used in other contexts too, in some of which stripping the separator may not seem as natural as when directly asking for user input. Jun 22, 2011 at 6:54
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.

2
  • 3
    I don't think that's it. Ruby is not designed as a fast language, and there is no gain if everyone has to chomp the string anyway.
    – steenslag
    Jun 21, 2011 at 22:20
  • Just because Ruby is extremely expressive doesn't mean you can't consider speed. And like I said, "nobody needs the newline" != "everybody has to chomp".
    – Coderer
    Jun 21, 2011 at 22:36
0

How I auto-detect line endings:

# file open in binary mode
line_ending = 13.chr + 10.chr
check = file.read(1000)
case check
when /\r\n/
  # already set
when /\n/
  line_ending = 10.chr
when /\r/
  line_ending = 13.chr
end
file.rewind

while !file.eof?
  line = file.gets(line_ending).chomp
  ...
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.