Forum: Ruby Reading bytes from a file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aaron P. (Guest)
on 2007-01-03 21:30
(Received via mailing list)
I'm trying to read bytes from a file, and it would be handy if there was
a method that threw an exception if I couldn't read that many bytes, or
encountered an EOF.

IO#readbytes seems like the perfect method, but I have an instance of
File.
The documentation says that File inherits from IO, but the File instance
doesn't seem to have the readbytes method.  Am I missing something?

--Aaron
Aaron P. (Guest)
on 2007-01-03 21:34
(Received via mailing list)
Oops.....  Just needed to require 'readbytes'

--Aaron
William J. (Guest)
on 2007-01-03 22:46
(Received via mailing list)
Aaron P. wrote:
> I'm trying to read bytes from a file, and it would be handy if there was
> a method that threw an exception if I couldn't read that many bytes, or
> encountered an EOF.
>
> IO#readbytes seems like the perfect method, but I have an instance of File.
> The documentation says that File inherits from IO, but the File instance
> doesn't seem to have the readbytes method.  Am I missing something?

s = open('junk'){|f| f.read(10) }
fail   if s.size < 10
This topic is locked and can not be replied to.