Alex Rivera | Logout

What is an EOFError in Ruby file I/O?

Asked 2009-08-13T14:41:48.330
13

The official documentation doesn't specify. I understand EOFError means "End of file error", but what exactly does that mean? If a file reader reaches the end of a file, that doesn't sound like an error to me.

Edit
Report

1 Answer

20

EOFError is handy in all of IO, the class which is the basis of all input/output in ruby. Now also remember core Unix concepts: everything is a file. This includes sockets. So, if you have some socket open and are reading from it, an exceptional condition might be to encounter an end of file.

All the examples out there show trivial uses of EOFError (while reading some text file), which are not really useful. However, start digging through net/http or other classes which use sockets heavily, and you'll see this exception being used.

Edited to add this example from net/ftp

def getline
  line = @sock.readline # if get EOF, raise EOFError
  line.sub!(/(\r\n|\n|\r)\z/n, "")
  if @debug_mode
    print "get: ", sanitize(line), "\n"
  end
  return line
end
answered 2009-08-13T15:29:03.487

Your Answer