Question

I am writing a script to read the header bytes from a PNG file. I want to use the readbytes method on File:

f = File.open("Boots.png", "rb:binary")
header = f.readbytes(8)

But I get a NoMethodError on the second line:

NoMethodError: undefined method `readbytes` for #<File:Boots.png>
    from (irb):2
    from #:0

Why? As far as I can tell from the doc, readbytes is part of the IO class, parent to File, and should be available to me, without a require or include. I am almost exactly following a sample in the David Flanagan guide; I can even find the source readbytes.rb file in my Ruby installation.

Note that I am running the MRI 1.8.7 on Windows 7.

Was it helpful?

Solution

readbytes isn't member of IO or File (where did you find the entry in the doc?). Use IO#read([length]) to read bytes from file. And you may need String#unpack to convert the string to magic header you want to compare.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top