eof? method from File object tells if the file reading reaches the file end:
f = File.new("test.txt", "r") catch(:end_of_file) do # from w ww .j a v a 2s.c om loop do throw :end_of_file if f.eof? puts f.gets end end f.close
Here, we use an "infinite" loop that you break out of by using catch and throw.
throw is only called if the file pointer is at or past the end of the file.