On Tue, 6 May 2003 23:18:29 +0900, dblack / superlink.net wrote:
> On Tue, 6 May 2003, Austin Ziegler wrote:
>> 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?
> I approach this with trepidation, because (un)pack is one of these
> things that I have to look up and talk myself through every time I
> use it... but, for what little it's worth, I always thought there
> was a pretty direct and sensible idea behind this, namely that one
> packed things into a string and unpacked them into an array.

I have to look up the template meanings most times, myself, but I
personally think of "pack" as packing binary data. I think that
looking at this as pack-from-array and unpack-to-array is not
necessarily productive.

  "Austin".unpack("A*") # => ["Austin"]

There are others where this is the case, too. Not everything will
be:

  "Austin".unpack("c*") # => [65, 117, 115, 116, 105, 110]

I'm finding that the code I'm working with has several cases where I
could use String#pack or Fixnum#pack because I'm having to do:

  [foo].pack(template)

.. and it's in loops, so the minimal inefficiency in creating an
anonymous Array will add up over time. (I don't have anything that I
can profile at this point, so I can't *prove* that it's
inefficient.)

I don't think that this is too "magic"; I think that having to
create an anonymous array feels wrong, personally.

BTW, Brian, the inefficiency of creating an anonymous Array -- in a
loop -- is precisely why I want String#pack and Fixnum#pack (I don't
want a generic #pack); a Float#pack may be useful.

I think that it could be possible to have these be named differently
(per Matz's suggestion); perhaps #encode? I donno.

-austin
-- Austin Ziegler, austin / halostatue.ca on 2003.05.06 at 16:38:07