From: "Einar Magnús Boson" Date: 2008-12-17T15:02:05+09:00 Subject: Re: ruby way of dumping file On 17.12.2008, at 05:42 , The Higgs bozo wrote: > Joshua Ball wrote: >> Hi, >> Can someone give me a cleaner, more rubyesque (even correct) way of >> printing >> out the first 20 bytes of a file? My solution doesn't feel right. > > print File.read(ARGV.first)[0...20] > -- > Posted via http://www.ruby-forum.com/. > Doesn't that read the entire file before extracting the first 20 bytes? Seems silly to me. rather something like this: open ARGV.first, "rb" do |f| read = "" begin read += f.read(20) end while read.size < 20 && !f.eof? p read[0...20] end although I suppose this would work too: p open(ARGV[0], "rb"){|f|f.read(20)} but I don't think read(20) is guaranteed to read 20 bytes but maybe that is just in sockets. einarmagnus