From: Brian Candler Date: 2003-05-06T19:54:07+09:00 Subject: Re: Suggestion: String#pack On Tue, May 06, 2003 at 01:55:51PM +0900, Austin Ziegler wrote: > I have been working on some code recently where it would be very > useful to do: > > "abcdef".pack(...) > > Instead, I have to do: > > ["abcdef"].pack(...) > > This is guaranteed to be at least a minor inefficiency because a > temporary anonymous array needs to be created. Is there a good reason > why String can't have a #pack method that Does The Right Thing? Well, there are many other data types that you might want to pack (Fixnum and Bignum for example). Should they have a #pack method as well? A common example is packing integers into 4-byte strings: [1234].pack("N") # OK 1234.pack("N") # ?? This becomes a bit like 'to_yaml' where ultimately everything has this method, including Object, which I don't really like. Maybe it would be better to turn it around and make module methods for packing individual items: e.g. Pack.pack(1234,"N") or Pack.n4(1234) # network byte order, 4 bytes Hmm, lots of methods though. If you don't care about the slight inefficiency and just want the cleaner interface, how about: class Object def pack(arg) [self].pack(arg) end end Regards, Brian.