From: Ben Nagy Date: 2006-06-05T16:52:19+09:00 Subject: Like BitStruct, but better? Hi all, So what I'm really looking for is something that is like the (reasonably cool) bit-struct library, but with some improvements. I don't mind hacking a bunch of stuff onto a more generic format / library if I have to. What I need is a way to rapidly build classes that represent structured, binary data. Bit-struct only lets me define fields up to 32 bits long, which is bad, and there isn't (AFAICT) a way to have variable length fields in mid-structure. As an example I might want to represent a protocol header which has a Length field followed by some data of the appropriate length, and then followed by some more fixed structure elements. I should mention that for my _particular_ needs I need a way to easily set invalid lengths, too. Could be as simple a obj.variable_value= automatically updating the length field whereas obj.len_field= sets length manually. An easy way to marshal / unmarshal (pack unpack, whatever) data according to field-type would be a bonus. In an ideal world the interface would be very similar to bit-stuct where you write class definitions which get interpreted by meta-magic to create a real class with all the accessors ready made, but with the added ability to define :len_of_field8 as :value_of_field8.length, and unlimited fixed bit-length fields. Elegant ideas for the representation of the structure definitions and the general direction to take for the class construction code would be most helpful. I've only started hacking with ruby recently, but I'm finding it fun as hell. I could write a solution for myself, but I figure if I ask I might get a hint towards doing things a more idiomatic way. :) Cheers, ben