From: Bill Kelly Date: 2009-11-28T09:26:36+09:00 Subject: Re: Ruby byte access to disk sectors like dd does From: "Robert Klemme" > > 2009/11/27 Bill Kelly : >> >> ruby -e 'File.open("/dev/hdb", "rb") {|io| print(io.read(512))}' | hexdump -C > > Just a small remark: when using #read for reading I would prefer to > also use #write for writing. It is more symmetric and It may actually > be that #print does a bit more in light of i18n support in Ruby 1.9. > So that would be > > ruby -e 'File.open("/dev/hdb", "rb") {|io| > $stdout.write(io.read(512))}' | hexdump -C Hmm. From my point of view, symmetry-for-symmetry's sake can't generally apply here, because, for example, I would expect the following to be bad form: $stdout.syswrite(io.sysread(512))}' | hexdump -C I.e., since 'io' and stdout are separate streams, I may choose to call *only* 'sys' methods on 'io', but I can't in turn reasonably expect to call corresponding 'sys' methods on the (presumably buffered) $stdout ... However, your observation did make me curious as to how different 'print' might be from 'write' vis-a-vis m17n on ruby 1.9.x. In the 1.9.2dev sources, it appears 'write' and 'print' are nearly identical (rb_io_print calls rb_io_write), but, that each argument to print will be followed by the "output record separator" string, if it is non-nil. ( $\ ) So if we wish to avoid the possibility of $\ contaminating our output, we indeed should call write instead of print. > The more fun solution would of course be to code the hex dumping in > Ruby also. :-) :) Here's a stab at it... (it does work properly if the last read returns fewer than 16 bytes.) 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' Regards, Bill