Read only a few bytes from file

Is there any way to read only a determined amount of bytes from a
specific file? I actually want to do that to a URL, but I assume the
procedure would be the same.

If someone could at least point me to the right direction that would
be great!

Thanks

[email protected] wrote:

Is there any way to read only a determined amount of bytes from a
specific file? I actually want to do that to a URL, but I assume the
procedure would be the same.

If someone could at least point me to the right direction that would
be great!

If you want to read from the start of the file, File#read takes a number
of bytes as a parameter:

irb(main):015:0> File.open(’/usr/share/dict/words’){|f| f.read(50)}
=> “\nA\nA’s\nAOL\nAOL’s\nAachen\nAachen’s\nAaliyah\nAaliyah’s”

On Mar 25, 2007, at 7:10 AM, [email protected] wrote:

Is there any way to read only a determined amount of bytes from a
specific file? I actually want to do that to a URL, but I assume the
procedure would be the same.

If someone could at least point me to the right direction that would
be great!

Thanks

Initial contents of http://conferences.oreillynet.com/rails/

RailsConf 2007 • May 17, 2007 - May 20, 2007 • Portland, Oregon
 <meta name="description" content="RailsConf 2007 - May 17, 2007
  • May 20, 2007 - Portland, Oregon" />

$ irb

require ‘open-uri’
=> true

open(‘http://conferences.oreillynet.com/rails/’) { |f|
?> f.pos = 182

puts f.read(14)
}
RailsConf 2007
=> nil

open-uri extends Kernel#open to understand how to read from HTTP and
FTP sources
IO#pos= sets the offset
IO#read returns up to the number of bytes requested

-Rob

Rob B. http://agileconsultingllc.com
removed_[email protected]

[email protected] wrote:

This works, however it still reads the entire page but only displays
the certain amount of bytes. Is it possible to only load the amount of
bytes that I specify?

Say for instance that I have a 10mb file, how do I get the first few
bytes of this file without having to read it entirely first?

Specify the number of bytes you want to the read method. Because of
buffering read may actually read more bytes than you want, but it won’t
be all 10mb.

On Mar 25, 9:59 am, Rob B. [email protected]
wrote:

 <meta name="description" content="RailsConf 2007 - May 17, 2007  

=> true
IO#read returns up to the number of bytes requested

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This works, however it still reads the entire page but only displays
the certain amount of bytes. Is it possible to only load the amount of
bytes that I specify?

Say for instance that I have a 10mb file, how do I get the first few
bytes of this file without having to read it entirely first?

Thanks

On Mar 25, 2007, at 6:15 PM, [email protected] wrote:

I tested on a URL with around 200kb and it took the exact same time
reading it all vs reading only 8 bytes. I believe this is done in PHP
via sockets, isn’t the same possible with Ruby?

Absolutely.

require ‘open-uri’

text = open(“http://www.google.com”) { |f| f.read(8) }

puts “first eight bytes is: #{text}”

Gary W.

On Mar 25, 6:49 pm, Timothy H. [email protected] wrote:

be all 10mb.
I tested on a URL with around 200kb and it took the exact same time
reading it all vs reading only 8 bytes. I believe this is done in PHP
via sockets, isn’t the same possible with Ruby?