[RCR]: Replace pack and unpack with OO versions

Discussion in 'Ruby' started by Bill Atkins, Jun 14, 2004.

  1. Bill Atkins

    Bill Atkins Guest

    Rather than having code like this:

    flags, ignore, length, name = data.unpack "nnna*"

    let's try this:

    u = Unpacker.new data
    flags, ignore, length, name = u.word, u.word, u.word, u.string

    I think this looks a lot neater and is much more maintainable. A
    similar thing could be done with pack.

    p = Packer.new
    p.word 0x01
    p.word 0x00
    p.word 0x08
    p.string "ERERER"

    p.to_s => packed version

    Any ideas on this before I draft it up into a real RCR?

    Bill Atkins
    Bill Atkins, Jun 14, 2004
    #1
    1. Advertising

  2. Bill Atkins

    Jamis Buck Guest

    Bill Atkins wrote:
    > Rather than having code like this:
    >
    > flags, ignore, length, name = data.unpack "nnna*"
    >
    > let's try this:
    >
    > u = Unpacker.new data
    > flags, ignore, length, name = u.word, u.word, u.word, u.string
    >
    > I think this looks a lot neater and is much more maintainable. A
    > similar thing could be done with pack.
    >
    > p = Packer.new
    > p.word 0x01
    > p.word 0x00
    > p.word 0x08
    > p.string "ERERER"
    >
    > p.to_s => packed version
    >
    > Any ideas on this before I draft it up into a real RCR?
    >
    > Bill Atkins
    >
    > .
    >


    I'm for it. I actually wrote up a couple of utility classes to (more or
    less) do just this for me, for the Net::SSH stuff. Dealing directly with
    pack/unpack makes my head hurt after a while.

    --
    Jamis Buck

    http://www.jamisbuck.org/jamis

    ruby -h | ruby -e
    'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a <<
    r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'
    Jamis Buck, Jun 14, 2004
    #2
    1. Advertising

  3. Bill Atkins

    Hal Fulton Guest

    Bill Atkins wrote:
    > Rather than having code like this:
    >
    > flags, ignore, length, name = data.unpack "nnna*"
    >
    > let's try this:
    >
    > u = Unpacker.new data
    > flags, ignore, length, name = u.word, u.word, u.word, u.string
    >
    > I think this looks a lot neater and is much more maintainable. A
    > similar thing could be done with pack.
    >
    > p = Packer.new
    > p.word 0x01
    > p.word 0x00
    > p.word 0x08
    > p.string "ERERER"
    >
    > p.to_s => packed version
    >
    > Any ideas on this before I draft it up into a real RCR?


    Hmm, looks like it is keeping some internal state? A "file
    pointer" so to speak?


    Hal
    Hal Fulton, Jun 14, 2004
    #3
  4. Bill Atkins <> wrote:
    > Rather than having code like this:
    >
    > flags, ignore, length, name = data.unpack "nnna*"
    >
    > let's try this:
    >
    > u = Unpacker.new data
    > flags, ignore, length, name = u.word, u.word, u.word, u.string
    >
    > I think this looks a lot neater and is much more maintainable. A
    > similar thing could be done with pack.


    Alternatively, something I've been thinking of is to let pack and unpack
    take array arguments, which are 'compiled' into pack/unpack strings.

    e.g. flags, ignore, length, name = u.unpack [:word, :word, :word,
    :string]

    All the compiler would have to do is translate symbols, and pass numbers
    through unchanged. Or, if you wanted to construct it in terms of pairs
    like [[:string, 3]], just run a flatten first.

    martin
    Martin DeMello, Jun 14, 2004
    #4
  5. On Mon, Jun 14, 2004 at 02:48:38PM +0900, Martin DeMello wrote:
    > Bill Atkins <> wrote:
    > > Rather than having code like this:
    > >
    > > flags, ignore, length, name = data.unpack "nnna*"
    > >
    > > let's try this:
    > >
    > > u = Unpacker.new data
    > > flags, ignore, length, name = u.word, u.word, u.word, u.string
    > >
    > > I think this looks a lot neater and is much more maintainable. A
    > > similar thing could be done with pack.

    >
    > Alternatively, something I've been thinking of is to let pack and unpack
    > take array arguments, which are 'compiled' into pack/unpack strings.
    >
    > e.g. flags, ignore, length, name = u.unpack [:word, :word, :word,
    > :string]
    >
    > All the compiler would have to do is translate symbols, and pass numbers
    > through unchanged. Or, if you wanted to construct it in terms of pairs
    > like [[:string, 3]], just run a flatten first.


    It would be nice if Packer/Unpacker would work on IO/File objects. I've
    written a BinaryReader and BinaryWriter mixin module, with methods like:

    read_word8(byte_order=ByteOrder::Native)
    read_word8_big ..._network
    read_word8_little
    read_word8_native

    and the same for word16, word32, int8, int16...

    int's are signed integers, word are unsigned.

    And to use it, you just have to extend your io object:

    require 'stringio'
    io = StringIO.new("Hello World")
    io.extend(BinaryReader)
    p io.read_word32

    It's easy to add a unpack method:

    class BinaryReader
    def unpack(*spec)
    spec.map {|s| send("read_#{s}")}
    end
    end

    io.unpack:)word8, :word8, :word32_little)


    Regards,

    Michael
    Michael Neumann, Jun 14, 2004
    #5
  6. Bill Atkins

    Bill Atkins Guest

    Yeah, that's what I was figuring. I assumed that packing would
    prepend rather than append, but I suppose that's configurable.

    Hal Fulton <> wrote in message news:<>...
    > Bill Atkins wrote:
    > > Rather than having code like this:
    > >
    > > flags, ignore, length, name = data.unpack "nnna*"
    > >
    > > let's try this:
    > >
    > > u = Unpacker.new data
    > > flags, ignore, length, name = u.word, u.word, u.word, u.string
    > >
    > > I think this looks a lot neater and is much more maintainable. A
    > > similar thing could be done with pack.
    > >
    > > p = Packer.new
    > > p.word 0x01
    > > p.word 0x00
    > > p.word 0x08
    > > p.string "ERERER"
    > >
    > > p.to_s => packed version
    > >
    > > Any ideas on this before I draft it up into a real RCR?

    >
    > Hmm, looks like it is keeping some internal state? A "file
    > pointer" so to speak?
    >
    >
    > Hal
    Bill Atkins, Jun 14, 2004
    #6
  7. (Bill Atkins) wrote in message news:<>...
    > Rather than having code like this:
    >
    > flags, ignore, length, name = data.unpack "nnna*"
    >
    > let's try this:
    >
    > u = Unpacker.new data
    > flags, ignore, length, name = u.word, u.word, u.word, u.string
    >
    > I think this looks a lot neater and is much more maintainable. A
    > similar thing could be done with pack.
    >
    > p = Packer.new
    > p.word 0x01
    > p.word 0x00
    > p.word 0x08
    > p.string "ERERER"
    >
    > p.to_s => packed version
    >
    > Any ideas on this before I draft it up into a real RCR?
    >
    > Bill Atkins


    I mentioned this a *long* time ago, but didn't get much feedback. If
    I recall, I was inspired in part by the port of
    Spreadsheet::WriteExcel, in which there's a lot of pack/unpack going
    on.

    http://ruby-talk.com/cgi-bin/scat.rb/ruby/ruby-talk/44847

    I definitely support the idea.

    Regards,

    Dan
    Daniel Berger, Jun 14, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joe shoemaker

    Python pack and unpack question

    joe shoemaker, Jul 15, 2008, in forum: Python
    Replies:
    0
    Views:
    309
    joe shoemaker
    Jul 15, 2008
  2. Anton Hörnquist

    Array#pack and String#unpack query

    Anton Hörnquist, Sep 11, 2008, in forum: Ruby
    Replies:
    2
    Views:
    104
    Jan Dvorak
    Sep 11, 2008
  3. Tulan W. Hu

    pack and unpack question for perl 5.8.0

    Tulan W. Hu, Jul 28, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    102
    Tulan W. Hu
    Jul 28, 2003
  4. Steve

    pack and unpack ?

    Steve, Jan 9, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    118
  5. Kim Schulz

    pack and unpack integer data

    Kim Schulz, Nov 1, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    125
    Kim Schulz
    Nov 2, 2005
Loading...

Share This Page