From: Brian Candler Date: 2008-12-17T20:35:45+09:00 Subject: Re: ruby way of dumping file Einar Magnç‚­s Boson wrote: > 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. In Ruby, I believe it's guaranteed to read 20 bytes, from both files and sockets - it will restart the underlying OS calls if necessary - unless EOF is reached. I think the each_byte approach is cleanest, but here are some alternatives: # not ruby-1.9 clean puts File.read(ARGV[0],20).gsub(/(.)/) { "0x%02x\n" % $1[0] } # different output format puts File.read(ARGV[0],20).unpack("H*")[0].scan(/../).join(" ") puts File.read(ARGV[0],20).unpack("H2 "*20).join(" ") -- Posted via http://www.ruby-forum.com/.