From: Garthy D Date: 2012-01-19T09:41:05+09:00 Subject: Re: Extending/embedding Ruby Hi Mat��j P, I might be able to offer a few starting points. I'll just pre-warn you that I'm not in any way an expert on this stuff! On 19/01/12 08:53, Mat��j P. wrote: > Hi! I would like to extend nfdump in Ruby, which will be probably > challenge for me, because I have never write any extension. > What is core thing is, that nfdump reads binary file with huge amount of > data. This reading and all other job I want to do nfdump which is in C > and dont have library files, so I cant use ffi or something like that. > Nfdump reads file header, makes a structure represents a netflow file > and than it reads n flow records which is represented by struct in which > is another struct and variables. > I need to get this structure to Ruby. > My first question is, if its possible to get struct in which is other > struct etc. to Ruby. You can wrap up opaque data (eg. C structures) using Data_Wrap_Struct, and it back using Data_Get_Struct. You can then define a number of calls (using rb_define_method) to extract relevant values from your structures. > Second is about approach. I have some problems with embedding Ruby > Interpreter in C- example from Programming in Ruby fails on SegFault. I've personally found "Programming Ruby - The Pragmatic Programmers' Guide", in the chapter "Extending Ruby" to be the best source of information on embedding Ruby. It's not easy- in fact, I've embedded Lua and Python and they were far easier to get working, but once I'd got it going, I found Ruby to be more rewarding (since I vastly prefer it over Lua and Python). As for the seg fault, you might need to give a bit more info for someone to help you. :) > So > do you thing it is possible to write this as Ruby extension? > In the case of Ruby extension I didnt see in any example how to get to > Ruby struct which is inicialized in C. I cant inicialize it in Ruby > because its a lot of data and I dont know how many flows are in file. > I > cant give structs to array because of memory problem and also I need to > work with record as soon as its read from the file by C. > Your suggestion are highly welcomed. Thanks in advance. Data_Wrap_Struct will probably solve your problem. You can create your object in C, and then wrap it up in a Ruby object. I hope this helps. Garth