# Bit-struct and arrays of members?

**URL:** <https://www.ruby-forum.com/t/bit-struct-and-arrays-of-members/168719>\
**Category:** Ruby\
**Created:** [May 25, 2009, 12:01am UTC](https://www.ruby-forum.com/t/bit-struct-and-arrays-of-members/168719 "2009-05-25T00:01:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [May 25, 2009, 12:01am UTC](https://www.ruby-forum.com/t/bit-struct-and-arrays-of-members/168719/1 "2009-05-25T00:01:00Z")

</div>

Hi,

I’m using the bit-struct [1] (which is just awesome, btw) and wondering  
how I can realize arrays of members.

I’m reading binary data from pre-existing files which are based on C  
structs which look similar to this basic example:

struct {  
int vector[5];  
…  
} vector\_t;

My current solution is to just write:

class Vector \< BitStruct  
default\_options :endian =\> :little  
signed vector\_1, 32  
signed vector\_2, 32  
signed vector\_3, 32  
…

I would prefer to use an array. I couldn’t figure out how to do it,  
there’s no example with it and I start to think it’s not possible … ?

thanks,

- Markus

[1] [BitStruct](http://redshift.sourceforge.net/bit-struct/)

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [May 25, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/bit-struct-and-arrays-of-members/168719/2 "2009-05-25T21:46:43Z")

</div>

Markus F. wrote:

> …
> 
> I would prefer to use an array. I couldn’t figure out how to do it,  
> there’s no example with it and I start to think it’s not possible … ?
> 
> thanks,
> 
> - Markus
> 
> [1] [BitStruct](http://redshift.sourceforge.net/bit-struct/)

Sorry, no support for that (yet). IIRC one of the other libs has  
something for this, maybe it was binstruct or binaryparse.

It’s not really too different from the #nest construct, so maybe it  
could be added along those lines, as long as the array length is static.  
Will think about it…

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [May 27, 2009, 2:33am UTC](https://www.ruby-forum.com/t/bit-struct-and-arrays-of-members/168719/3 "2009-05-27T02:33:48Z")

</div>

Markus F. wrote:

> I’m using the bit-struct [1] (which is just awesome, btw) and wondering  
> how I can realize arrays of members.

Here’s a quick implementation and some examples. The main limitation  
(that I’m aware of) is that a Vector cannot yet be embedded in another  
BitStruct. Will think about that (note hypothetical syntax)…

[http://pastebin.com/ma74d15d](http://pastebin.com/ma74d15d)
