From: Quintus Date: 2012-05-08T17:10:14+09:00 Subject: Re: How to use Data_Wrap_Struct to assign the DATA VALUE to an exsiting Ruby object? Am 08.05.2012 00:21, schrieb I��aki Baz Castillo: > Hi, my code receives an arbitrary klass name (provided by the user) > and I need to create an instance of such a klass and also wrap a C > pointer within it. > > Unfortunately Data_Wrap_Struct(VALUE class, void (*mark)(), void > (*free)(), void *ptr") requires a class VALUE rather than a single > object/instance. So I expect that I *cannot* attach/wrap a C DATA > pointer into an *existing* Ruby object and, instead, I need to store > the DATA pointer within an internal attribute of the object, am I > right? > > Thanks a lot. > No, it���s possible, but not documented. I���m not entirely sure how I did this, but it involved reading the bare RDATA struct from the the object and changing its internal pointer directly (maybe you should try hunting around in ruby.h/intern.h for the exact struct definitions). My usecase however was a bit different; I had to place a pointer already at the class instanciation, and then replace it later with a pointer to something different, i.e. my object was already wrapping something. I���m not sure if the trick via RDATA does work with objects that haven���t been assigned something right from the beginning. Vale, Marvin