Broken pack/unpack workaround

H

Hans Fugal

Array#pack and String#unpack are broken on OS X Tiger's ruby build. This
is known. I'm writing a lib where pack and unpack are used extensively
and would like to provide a workaround. Does anyone have such a
workaround already worked out? I can't think of a real easy way to do it
so I thought I'd check before running the gauntlet.

See also http://hans.fugal.net/yodl/blosxom.cgi/mac/ruby-pack.html
 
P

Paul Battley

Here's a simplistic solution:

broken =3D (1 =3D=3D [1].pack('n')[0])
if broken
class String
alias_method :broken_unpack, :unpack
def unpack(spec)
return broken_unpack(spec.tr('nvNV', 'vnVN'))
end
end
class Array
alias_method :broken_pack, :pack
def pack(spec)
return broken_pack(spec.tr('nvNV', 'vnVN'))
end
end
end

p([1].pack('n'))
p([1].pack('v'))
p([1].pack('N'))
p([1].pack('V'))

Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top