2

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.

aalavi
  • 63
  • 5
  • Why not store the Proc VALUE as an instance variable of your `ObjExt` instance? – thomthom Jan 01 '14 at 16:50
  • Sure, can do that. But then how to pass this VALUE and run it through (*init)(void) to manipulate tObj structure? Ruby wrapper is for test purpose. This struct will be used in embedded system without ruby wrapper. There, (*init)(void) would do something based on voltage of a pin. – aalavi Jan 03 '14 at 23:38

0 Answers0