Project

General

Profile

Actions

Bug #822

closed

gets blocks when it shouldn't

Bug #822: gets blocks when it shouldn't

Added by saeed (Ahmed Saeed) almost 17 years ago. Updated over 14 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
Backport:
[ruby-core:20310]

Description

=begin
r,w = IO.pipe
w.write("some text\n\n\nsome text\n\n\n")
puts r.gets("")
puts r.gets("")

When I used this code gets blocks on the second call. When the last newline character is replaced with any other character it works just fine.
=end

Updated by nobu (Nobuyoshi Nakada) almost 17 years ago Actions #1

  • Status changed from Open to Rejected

=begin
It's spec.
IO#gets with an empty string swallows empty lines, read non-empty lines, and swallows empty lines again, so that next #gets can read next non-empty line.
You have to close w to let r know EOF.
=end

Updated by candlerb (Brian Candler) almost 17 years ago Actions #2

=begin
Maybe this is a documentation error then. ri IO.gets only says: "two successive newlines in the input separate paragraphs". Perhaps it should say "a sequence of two or more successive newlines"? And also mention that such a sequence is replaced with two newlines?

r,w = IO.pipe
w.write("some text\n\n\n\n\nsome text\n\n\n\n\n")
w.close
puts r.gets("").inspect # => "some text\n\n"
puts r.gets("").inspect # => "some text\n\n"

=end

Updated by nobu (Nobuyoshi Nakada) almost 17 years ago Actions #3

=begin
Hi,

At Fri, 5 Dec 2008 04:39:12 +0900,
Brian Candler wrote in [ruby-core:20318]:

Maybe this is a documentation error then. ri IO.gets only
says: "two successive newlines in the input separate
paragraphs". Perhaps it should say "a sequence of two or more
successive newlines"?

Perhaps.

And also mention that such a sequence is replaced with two
newlines?

It is "dropped" rather than "replaced".

How do you think this?


Index: io.c

--- io.c (revision 20531)
+++ io.c (working copy)
@@ -1771,5 +1771,6 @@ rb_io_gets(io)

  • sep_string. A separator of nil reads the entire
  • contents, and a zero-length separator reads the input a paragraph at
    • a time (two successive newlines in the input separate paragraphs).
    • a time (two successive newlines in the input separate paragraphs,
    • and following successive newlines are dropped).
    • The stream must be opened for reading or an IOError
    • will be raised. The line read in will be returned and also assigned

--
Nobu Nakada

=end

Updated by candlerb (Brian Candler) almost 17 years ago Actions #4

=begin
On Fri, Dec 05, 2008 at 10:40:56AM +0900, Nobuyoshi Nakada wrote:

And also mention that such a sequence is replaced with two
newlines?

It is "dropped" rather than "replaced".

How do you think this?
..

    • a time (two successive newlines in the input separate paragraphs,
    • and following successive newlines are dropped).

Better, but now it doesn't say that preceding newlines are dropped.

r,w = IO.pipe
w.write("\n\n\n\n\nsome text\n\n\n\n\nmore text\n\n\n\n\nabc\n")
puts r.gets("").inspect
puts r.gets("").inspect
puts r.gets.inspect

Outputs:

"some text\n\n"
"more text\n\n"
"abc\n"

I'm not sure how best to describe this, except algorithmically:

When reading a 'paragraph':

  • any initial newlines are discarded
  • data is read up to and including two successive newlines
  • any subsequent newlines are discarded
  • the read data is returned

=end

Actions

Also available in: PDF Atom