From: Laurent Julliard Date: 2002-02-18T03:24:45+09:00 Subject: Accessing C structure members from Ruby I'm in the process of writing a Ruby Extension for a C library. This C library has a number of C structures which I'm going to use as a basis to create the corresponding Ruby Classes. So far so good. However instead of just using the Data_Wrap_Struct facility to encompass these structures I'd like to put in place a mechanism where each member of the original C structure has an equivalent instance variable in the corresponding Ruby Class. What is the best way to do this? I see two complementary approach for the moment but may be you'll have other ideas/tips: 1) I can put in place a copy mechanism in the C extension so that whenever a method is called it copies all the instance variables in the corresponding C structure members, call the C function, and copy the C struct members back to the Ruby instance variables when the C function returns. 2) In addition I could also define Ruby accessors (Get/Set) which are small C function which write to/ read from the appropriate C strcture member. I saw the rb_define_attr fucntion but unfortunately it looks like there is no way to hook accessors to "virtual" instance variables that would be actually be stored in C structure member. Am I missing something here? All comment/suggestion welcome. Laurent