# Reading bytes from a file

**URL:** <https://www.ruby-forum.com/t/reading-bytes-from-a-file/81094>\
**Category:** Ruby\
**Created:** [January 3, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/reading-bytes-from-a-file/81094 "2007-01-03T20:30:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [January 3, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/reading-bytes-from-a-file/81094/1 "2007-01-03T20:30:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [January 3, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/reading-bytes-from-a-file/81094/2 "2007-01-03T20:34:14Z")

</div>

Oops… Just needed to require ‘readbytes’

–Aaron

---

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [January 3, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/reading-bytes-from-a-file/81094/3 "2007-01-03T21:46:22Z")

</div>

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
