【发布时间】:2013-11-03 11:23:25
【问题描述】:
我正在编写一个脚本来从 PNG 文件中读取标头字节。我想在File 上使用readbytes 方法:
f = File.open("Boots.png", "rb:binary")
header = f.readbytes(8)
但我在第二行得到NoMethodError:
NoMethodError: undefined method `readbytes` for #<File:Boots.png>
from (irb):2
from #:0
为什么?据我从文档中可以看出,readbytes 是IO 类的一部分,是File 的父级,并且应该对我可用,不需要或包含。我几乎完全按照大卫弗拉纳根指南中的样本进行操作;我什至可以在我的 Ruby 安装中找到源文件 readbytes.rb。
请注意,我在 Windows 7 上运行 MRI 1.8.7。
【问题讨论】:
-
此时您不应该使用 Ruby 1.8。你应该切换到 Ruby 2.0。
-
事实证明
read()工作正常。