From: S Wayne Date: 2006-07-18T00:00:11+09:00 Subject: Re: First ruby code, need hint on iterator Keep a count of total characters processed, and output if that is the same as the length of your string: (see my embedded changes) Gerr wrote: > Hello, > > Consider the following code which generates a hexdump of a string: > > ---------------------------------------------------------------------- > > #!/usr/bin/ruby > > module Dump > > def hexdump > > x = 0 num_chars = 0 > offset = 0 > hex = "" > str = "" > out = "" > > each_byte { |c| > > x = x + 1 num_chars += 1 > > hex << sprintf("%02x ", c) > hex << "- " if(x == 8) > str << ((c >= 32 && c<=127) ? c.to_i.chr : ".") > if(x == 16) || num_chars >= length > out << sprintf("%08x: %-50s %s\n", offset, hex, str) > x = 0 > hex = "" > str = "" > offset = offset + 16 > end > } > > out << "\n" > > return out > end > end > > > l = $stdin.read(10000) > l.extend Dump > print l.hexdump >