Forum: Ruby Broken pack/unpack workaround

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ec594ddcaeae2771ee9d0dd7f0d598ed?d=identicon&s=25 Hans Fugal (Guest)
on 2006-03-13 16:31
(Received via mailing list)
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
2abf5beb51d5d66211d525a72c5cb39d?d=identicon&s=25 Paul Battley (Guest)
on 2006-03-13 17:48
(Received via mailing list)
Here's a simplistic solution:

broken = (1 == [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.
Ec594ddcaeae2771ee9d0dd7f0d598ed?d=identicon&s=25 Hans Fugal (Guest)
on 2006-03-13 17:54
(Received via mailing list)
Paul Battley wrote:

>       return broken_unpack(spec.tr('nvNV', 'vnVN'))

tr never occurred to me. That's perfect!
This topic is locked and can not be replied to.