From: gw Date: 2005-01-04T03:50:00+09:00 Subject: Re: wrapping Ruby around OLD code E S Wrote: >> L�hett�j�: gw >> 5 years ago one of our smart guys suggested we throw away our home grown "Basic" which glues our [SNIP] > >As long as the main manipulation of the data structures >happens in C, the following would apply: > >Instead of actually mapping the datatypes to Ruby, you can simply >wrap them inside opaque Ruby objects and pass them around that way. >Say you've defined a Ruby class 'MyData' (which in Ruby C is a >variable, traditionally c_MyData): > >// You'd normally call this in your C code >extern void do_something(struct whatever * ptr); > >#include 'ruby.h' > >// VALUE is the basic Ruby type in C >VALUE make_opaque_wrapper(struct whatever * ptr) { > // See 'Programming Ruby' > VALUE wrapper = Data_Wrap_Struct(c_MyData, 0, 0, ptr); > return wrapper; >} > >// Wrapper function to access the normal C functions again >void access_do_something(VALUE wrapper) { > // Extract the C structure > struct whatever * ptr = 0; > Data_Get_Struct(wrapper, struct whatever, ptr); > // Call the C function > do_something(ptr); > return; >} > >And so on. You'll want to read the links to Programming Ruby >given earlier. > >This way your Ruby interface can be pretty much anything you >need. If you need to use values from those data structures *in* >Ruby (for manipulation or whatever), you can just write extraction >functions to be called at those times. > >E > > Execellent advice, sir. Thank you for your considerate answer. George Wyche