Suggestion: String#pack

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?

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.05.06 at 00:53:14

Hi,

···

In message “Suggestion: String#pack” on 03/05/06, Austin Ziegler austin@halostatue.ca writes:

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(…)

Pack templates A, a, Z, B, b, H, h, u, m, P and p takes string
argument, so that it might be useful. But they are subset of pack
function. Do we have to name different name for different function?
String#pack might not be intuitive name for string padding/converting
function.

						matz.

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.

···

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?

Hi –

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?

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.

So the idea of packing a string into a string seems off-by-one to me,
logically – a little too “magic”. I’d put it (with the above
trepidation :slight_smile: in the same category as a hypothetical Array#split,
say, which auto-joined the array on ‘’ and then split the resulting
string.

David

···

On Tue, 6 May 2003, Austin Ziegler wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Brian Candler 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.

I had a similar idea a while ago:

Be sure to read the comments. Anyone want to submit a new RCR?

Regards,

Dan

···

On Tue, May 06, 2003 at 01:55:51PM +0900, Austin Ziegler wrote:

p
[“010100101010111011001110001011100000010010000010011101101111011000101110000101101010011001001110000001000100101010101110010001101001111000000100000100101000011011000110110101101010011001001110”].pack(“b*”)

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

···

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.

OK, understood. But I’m sure you know to beware the disease known as
“premature optimisation” :slight_smile:

Regards,

Brian.

···

On Wed, May 07, 2003 at 05:59:02AM +0900, Austin Ziegler wrote:

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.

Quoteing austin@halostatue.ca, on Wed, May 07, 2003 at 05:59:02AM +0900:

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.

Why not pre-create the Array:

packee =

loop {

packee[0] = foo
packee.pack(template)
}

Then instead of creating the Array, you’re just asigning to the
first element, which should be reasonably speedy, one hopes!

Would this not work?
Sam

···

On Tue, 6 May 2003 23:18:29 +0900, dblack@superlink.net wrote:

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