From: "Iñaki Baz Castillo" Date: 2012-05-08T20:36:32+09:00 Subject: Re: How to use Data_Wrap_Struct to assign the DATA VALUE to an exsiting Ruby object? Hi, thanks to both. Comments inline: 2012/5/8 Hans Mackowiak : > if you can use c++ in your binding you can use an std::map to > associate an ruby object with an other pointer I use C and honestly I wouldn't like to mix C++ stuff in my extension. > otherwise if you show us your code, we maybe find better ways Ok, my code does the following: 1) First in Ruby land the high-user calls a method by passing a class (klass). Such a class must include a specific module MyModule (my code checks it). 2) Then still in Ruby land, the method allocates an instance of such a klass: obj = klass.new 3) And the the method calls to another method defined in my Ruby C extension, by passing some extra arguments: obj._init_c_data(ip, port) 4) Note that the _init_c_data() method is defined for MyModule: rb_define_private_method(mMyModule, "_init_c_data", mMyModule_init_c_data, 2); 5) Within mMyModule_init_c_data C function, I need to allocate a DATA struct within "obj" (so "self" argument). Currently I create an internal attribute "@cdata" for "obj" and store there the DATA struct: cCData = rb_define_class("CData", rb_cObject); struct my_cdata* cdata = ALLOC(struct my_cdata); rb_ivar_set(self, rb_intern("@cdata"), Data_Wrap_Struct(cCData, NULL, NULL, cdata)); This works ok. However I wonder if it's possible to wrap the cdata struct within the "obj" object instead, but take into account that "obj" was previously allocated, and also note that the class of "obj" is provided by the high-user, so I cannot use rb_define_alloc_func() since I don't know the name of the klass. Thanks a lot. -- Iñaki Baz Castillo