Parsing binary data in Ruby

Viewed 922

We're doing a major rewrite of a project previously written in C into Ruby. We have a bunch of C structures, written as C typedefs:

struct my_struct {
    uint32_t foo;
    uint8_t bar;
    char baz[80];
}

Is there a quick way to load them all up in Ruby? For example, is there some way to convert these definitions into something that resembles code like

@foo = io.read(4).unpack('V')[0]
@bar = io.read(1).unpack('C')[0]
@baz = io.read(80)

There are literally tons of it, I'd rather not convert them by hand...

2 Answers
Related