Actions
Bug #21530
openIs IO#eof? supposed to always block and read?
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.
Actions
Like0
Like0Like0