
Issue #21530 has been reported by tenderlovemaking (Aaron Patterson). ---------------------------------------- Bug #21530: Is IO#eof? supposed to always block and read? https://bugs.ruby-lang.org/issues/21530 * Author: tenderlovemaking (Aaron Patterson) * Status: Open * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- I'm not sure whether or not this is expected behavior, but it seems like eof? blocks when called on $stdin. For example: ```ruby 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: ```c #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. -- https://bugs.ruby-lang.org/