Ruby IO#read 单次读取的最大长度

标签 ruby io

如何确定 IO#read 在当前平台上单次读取的最大长度?

irb(main):301:0> File.size('C:/large.file') / 1024 / 1024
=> 2145
irb(main):302:0> s = IO.read 'C:/large.file'
IOError: file too big for single read

最佳答案

该消息来自 io.c, remain_size .当文件的(剩余)大小大于或等于 LONG_MAX 时发出。该值取决于编译 Ruby 的平台。

至少在 Ruby 1.8.7 中,Fixnums 的最大值正好是该值的一半 (-1),因此您可以通过以下方式获得限制

2 * 2 ** (1..128).to_a.find { | i | (1 << i).kind_of? Bignum } - 1

你不应该依赖它。

关于Ruby IO#read 单次读取的最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7512075/

相关文章:

command-line - 如何顺序打印jq输出

c++ - 阻塞函数调用超时,即如何在X秒后停止等待用户输入?

ruby-on-rails - ruby rails : Custom routes for search results

ruby-on-rails - 如何链接到另一个用户的索引?

ruby-on-rails - 如何通知评论者?

asp.net - ASP.Net 调用的非托管 dll 无法在 IIS6 下创建文件

c++ - 输入什么时候停止?

javascript - 使用 Rspec : this code works but the spec can't find the route 测试 AJAX

c++ - 将 SWIG 与 C++ 模板函数结合使用

python - 将可变长度张量的集合保存到TensorFlow中的TFRecords文件中