Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

IO#eof?

eof?

Alias of #eof

Returns true if the stream is positioned at its end, false otherwise; see Position:

Example
f = File.open('t.txt')
f.eof           # => false
f.seek(0, :END) # => 0
f.eof           # => true

Raises an exception unless the stream is opened for reading; see Mode.

If self is a stream such as pipe or socket, this method blocks until the other end sends some data or closes it:

Example
r, w = IO.pipe
Thread.new { sleep 1; w.close }
r.eof? # => true # After 1-second wait.

r, w = IO.pipe
Thread.new { sleep 1; w.puts "a" }
r.eof?  # => false # After 1-second wait.

r, w = IO.pipe
r.eof?  # blocks forever

Note that this method reads data to the input byte buffer. So IO#sysread may not behave as you intend with IO#eof?, unless you call IO#rewind first (which is not available for some streams).

I#eof? is an alias for IO#eof.

See also #eof #erase_line #echo? #erase_screen also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0