0

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.

sawa
  • 165,429
  • 45
  • 277
  • 381
WaveformDelta
  • 141
  • 1
  • 8

1 Answers1

3

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.

Arie Xiao
  • 13,909
  • 3
  • 31
  • 30
  • Looks like there was such method in [Ruby 1.8](http://ruby-doc.org/stdlib-1.8.7/libdoc/readbytes/rdoc/IO.html). Perhaps it was deprecated in modern Ruby. – sawa Nov 03 '13 at 03:21
  • Thank you. @sawa, is there a way to definitely determine when it was deprecated? Note that I need to use 1.8.7--in my environment I don't have a choice. – WaveformDelta Nov 03 '13 at 16:25
  • @WaveformDelta Check the available methods by doing `p File.methods`. – sawa Nov 03 '13 at 16:30