Call a proc in Ruby C extension

191 views Asked by At

I have the following struct:

typedef struct{
    int a;
    int (*init)(void);
} tObj;

I am wrapping this into an object 'ObjExt' in Ruby. Ruby initialization method gets a Proc 'cb' that shall be run anytime 'init' function is called somewhere to generate an integer.

something like:

cb = Proc.new { 1 }
ruby_obj = ObjExt.new(cb)

My first shot at this was I passed the 'cb' proc to a global VALUE type variable and run rb_funcall on it in a wrapper function "int (*wrapper)(void)" that I define, and literally assign init = wrapper. but this won't work if I have multiple object instances of ObjExt class as the global variable is shared between instances and gets overwritten when initializing the second and third objects.

Any hints would be appreciated. I am probably approaching the problem in a wrong way.

0

There are 0 answers