From: Robert Klemme Date: 2009-11-29T22:00:11+09:00 Subject: Re: Ruby byte access to disk sectors like dd does On 28.11.2009 19:12, Bill Kelly wrote: > From: "Robert Klemme" >> On 11/28/2009 01:26 AM, Bill Kelly wrote: >>> :) Here's a stab at it... (it does work properly if the last >>> read returns fewer than 16 bytes.) >> What makes it stop working properly if the file size is a multiple of 16? > > It works OK in either case, is what I was trying to say. :) Ah, OK. Thanks for the clarification! >>> ruby -e 'File.open("/dev/hdb", "rb") {|io| print(io.read(512))}' | >>> ruby -e 'i=0; while(x=ARGF.read(16)); puts("%08x %-32s %s" % [i, x.unpack("H*"), x.tr("^\040-\176",".")]); i+=16; end' >> Why do you separate this in two processes? I've munged it a bit... > > Same reason dd and hexdump are separate programs. > > http://www.faqs.org/docs/artu/ch01s06.html Well, but in light of that neither a Ruby replacement of dd or hexdump is needed as they do exist already and do their job properly - and likely even more efficient than Ruby. :-) Write a program that reads binary files and outputs them in a similar format as "hexdump -C" integrating features of "dd": The program needs to be able to understand a series of paired options "--start X --length Y" and "--start X --end Z" (X, Y, Z of course being integer numbers with obvious properties) which denote the regions of the stream to read. The program must be capable of reading from a pipe as well! Bonus points for accepting parameter "--block-size B" which determines the size of chunks read in one read operation. You may also invent any number of additional options which control formatting of the output. Source code must be shorter than 20 lines of code per additional option. ;-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/