ios.eof → true or false ios.eof? → true or false
Returns true if ios is at end of file that means there are no more data to read. The stream must be opened for reading or an IOError will be raised.
f = File.new("testfile") dummy = f.readlines f.eof #=> true
If ios is a stream such as pipe or socket, IO#eof? blocks until the other end sends some data or closes it.
r, w = IO.pipe Thread.new { sleep 1; w.close } r.eof? #=> true after 1 second blocking r, w = IO.pipe Thread.new { sleep 1; w.puts "a" } r.eof? #=> false after 1 second blocking r, w = IO.pipe r.eof? # blocks forever
Note that IO#eof? reads data to a input buffer. So IO#sysread doesn’t work with IO#eof?.
Source Code
/* * call-seq: * ios.eof => true or false * ios.eof? => true or false * * Returns true if <em>ios</em> is at end of file that means * there are no more data to read. * The stream must be opened for reading or an <code>IOError</code> will be * raised. * * f = File.new("testfile") * dummy = f.readlines * f.eof #=> true * * If <em>ios</em> is a stream such as pipe or socket, <code>IO#eof?</code> * blocks until the other end sends some data or closes it. * * r, w = IO.pipe * Thread.new { sleep 1; w.close } * r.eof? #=> true after 1 second blocking * * r, w = IO.pipe * Thread.new { sleep 1; w.puts "a" } * r.eof? #=> false after 1 second blocking * * r, w = IO.pipe * r.eof? # blocks forever * * Note that <code>IO#eof?</code> reads data to a input buffer. * So <code>IO#sysread</code> doesn't work with <code>IO#eof?</code>. */ VALUE rb_io_eof(io) VALUE io; { OpenFile *fptr; int ch; GetOpenFile(io, fptr); rb_io_check_readable(fptr); if (feof(fptr->f)) return Qtrue; if (READ_DATA_PENDING(fptr->f)) return Qfalse; READ_CHECK(fptr->f); clearerr(fptr->f); TRAP_BEG; ch = getc(fptr->f); TRAP_END; if (ch != EOF) { ungetc(ch, fptr->f); return Qfalse; } rb_io_check_closed(fptr); clearerr(fptr->f); return Qtrue; }
<code/>and<pre/>for code samples.