Project

General

Profile

Actions

Bug #21530

open

Is IO#eof? supposed to always block and read?

Added by tenderlovemaking (Aaron Patterson) about 10 hours ago. Updated about 4 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:122910]

Description

I'm not sure whether or not this is expected behavior, but it seems like eof? blocks when called on $stdin.

For example:

if (str = $stdin.gets)
  $stderr.puts "read #{str}"
end

if $stdin.eof? # this call waits for input
  $stderr.puts "stdin is eof"
end

I think this is kind of odd behavior because if you input a string but do not input a newline, then hit ^D twice, $stdin should be at EOF, but eof? will block and wait for input. If you hit ^D a third time, $stdin will be EOF, but if you input a different character it will not be EOF.

Compare this C program:

#include <stdio.h>
#include <stdlib.h>

#define BUF_SIZE 4096

int main(int argc, char *argv[]) {
    char buf[BUF_SIZE];
    if (fgets(buf, BUF_SIZE, stdin)) {
        fprintf(stderr, "read %s\n", buf);
    }

    if (feof(stdin)) { // Does not block
        fprintf(stderr, "stdin is EOF\n");
    }
}

If you hit ^D twice with this C program, feof will return true for stdin. I would have expected the Ruby program and the C program to behave similarly, but they don't. Is this expected? The documentation indeed says that eof? will read, but shouldn't the IO be at EOF after the second ^D?

Thank you.

Updated by nobu (Nobuyoshi Nakada) about 4 hours ago

It has been changed intentionally, AFAIR, to allow read from the tty twice.

Actions

Also available in: Atom PDF

Like0
Like0