什么是Ruby文件I / O中的EOFError? [英] What is an EOFError in Ruby file I/O?

查看:176
本文介绍了什么是Ruby文件I / O中的EOFError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方文档没有指定。我明白EOFError的意思是文件结束的错误,但究竟是什么意思?如果一个文件读取器到达一个文件的末尾,这听起来不像是一个错误。

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.

推荐答案

EOFError非常方便在所有的IO,所有的输入/输出在ruby的基础上的类。现在还记得Unix的核心概念:一切都是文件。这包括套接字。所以,如果你打开了一些socket并且正在读取它,那么一个特殊的情况可能会遇到一个文件的结尾。

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.

所有的例子都显示了微不足道的用法EOFError(在阅读一些文本文件时),这是没有用的。但是,开始通过net / http或其他使用套接字的类进行挖掘,你会看到使用这个异常。

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.

编辑从net / ftp添加这个例子

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

这篇关于什么是Ruby文件I / O中的EOFError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆