From: Daniel Martin Date: 2007-05-15T06:05:46+09:00 Subject: Re: SOLUTION QUIZ 123 # Huffman Encoder "Rick DeNatale" writes: > Extending this to binary data will involve making an input stream > class which reads the binary data and converts it into reasonably > sized binary string representations, and an output stream which > accumulates the string representation of the codes and writes them as > binary as enough data is produced. The huffman.rb file contains these > classes in an embrionic form. Note that I did this with just a few methods, that yielded strings of zeros and ones to other methods. The structure was like this: encoding: encode called encode_bits, passing in the input io stream encode_bits would yield strings of 0s and 1s into a block that was inside encode, which used pack to squish them into binary bytes. decoding: decode called decode_bits, which yielded strings of 0s and 1s that were read from the (binary) input - unpack was used to make those strings. I admit that I don't really like the separation of the logic here - it's not really parallel between encoding and decoding, for instance - but this works and didn't require that much code. -- s=%q( Daniel Martin -- martin@snowplow.org puts "s=%q(#{s})",s.to_a.last ) puts "s=%q(#{s})",s.to_a.last