Saturday, May 24, 2008

Ruby 1.9 C API - Modules

Creating a module:

VALUE module = rb_define_module("name");

Add methods/functions to a module:

rb_define_method_function(module, "name", c_function, argc);

Now the tricky part is that ruby adds a reference to the module when calling the function. c_function would look like this if it were to accept 1 parameter:

VALUE c_function(VALUE module, VALUE arg);

In this case the argc parameter in the rb_define function should be 1 not 2 as you would expect.

Define a constant:

rb_define_const(module_or_class, "NAME", value);

value is a VALUE.

No comments: