On Sep 20, 2009, at 4:41 PM, Mikel Lindsaar wrote:

> Hello all,
>
> Ruby 1.8.x can round trip a binary file with pack and unpack to
> base64.  Ruby 1.9 returns false.  Not sure why, googling and reading
> books leads me to believe this should be true in every version, can
> any shed some light on this?
>
> Ruby 1.8.6
>> data = File.read('spec/fixtures/attachments/test.png')
>> data == [data].pack( 'm' ).unpack( 'm' )[0]
> => true
>
> Ruby 1.8.7-p174
>> data = File.read('spec/fixtures/attachments/test.png')
>> data == [data].pack( 'm' ).unpack( 'm' )[0]
> => true
>
> Ruby 1.9.1-p129
>> data = File.read('spec/fixtures/attachments/test.png')
>> data == [data].pack( 'm' ).unpack( 'm' )[0]
> => false

The encoding of the string returned from unpack does not match the  
encoding of the string returned from File.read.

Try this:

     data = File.read ARGV[0]
     round_trip = [data].pack( 'm' ).unpack( 'm' )[0]
     p data.force_encoding(Encoding::BINARY) ==  
round_trip.force_encoding(Encoding::BINARY)

---
Aaron Patterson
http://tenderlovemaking.com