Friday, July 18, 2008

Ruby 1.9 C API - Object Instances


To Create a new instance of an object with class Class we will do this:

VALUE obj;

obj = rb_obj_alloc(Class); // Allocate space and create new instance
rb_obj_call_init(obj, 0, 0); // We call the initialize method for the object

Remember rb_obj_alloc() does not initialize the object and will seg fault if you use any custom data types in the object.

The 2nd and 3rd parameters of rb_obj_call_init() are the argument count (argc) and the constructor parameters array ( VALUE* ).

No comments: