Graham Nicholls wrote:
> Michael Neumann wrote:
> 
> 
>>Graham Nicholls wrote:
>>
>>>I mentioned before that I'm writing a tool to extract data from a
>>>(dataease)
>>>database file according to a schema.   In 'C' I'd do something like
>>>
>>>struct data
>>>{
>>>        char header 4;
>>>        int recno;
>>>        char name[30];
>>>        char add_1[30];
>>>        ...
>>>        unsigned int cr_limit ;
>>>} data;
>>>
>>>and if I set the compiler options right so the structure was closely
>>>packed, I could simply read the file into this structure, and print out
>>>the results using
>>
>>You need to read sizeof(data) bytes as a String, then unpack it.
>>
>>Simple example:
>>
>>   C:
>>
>>   struct data {
>>     long a;
>>     long b;
>>   };
>>
>>   Ruby:
>>
>>   str = handle.read(8)
>>   a, b = str.unpack('LL')
>>
>>Regards,
>>
>>   Michael
> 
> Excellent - just what I (think!) I want.  I looked at pack, but didn't think
> of unpack - you don't know what you don't know, IYSWIM.  The pickaxe book
> is great, but its a reference - I'd love to see "Text Processing in Ruby"
> or something similar (I've got "Text Processing in Python", but Ruby is
> much nicer , IMO, but don't crosspost to c.l.python!).  Thank you.

Also have a look at my BinaryReaderMixin. This allows you to use methods 
like 'read_int32' or 'read_int32_little' etc. instead.

http://www.ntecs.de/viewcvs/viewcvs/postgres-pr/trunk/lib/binary_reader.rb?rev=448&root=ruby-dbi&view=auto

Regards,

   Michael