On Sat, 25 Jan 2003 07:59:35 +0900, Daniel Carrera wrote:

> I'm struggling to write a module, with a method, in C.
> 
> With plain Ruby, I would do something like this:
> 
>>> module M
>>>   def M.five
>>>     return 5
>>>   end
>>> end
> => nil
>>> M.five
> => 5
> 
> So I thought that, in C, I could write:
> 
> #include "science.h"
> 
> static VALUE mScience_seven(VALUE self) {  return rb_float_new(7); }
> 
> VALUE mScience;
> 
> void Init_science() {         /* Module definition */
>   mScience = rb_define_module("Science");
> 
>   mScience_define_constants();
>   rb_define_method(mScience, "Science.seven", mScience_seven, 0);
> }
> 
> The module itself builds, and I can access the constants fine.  But the 
> method is giving me trouble:
> 
>>> require 'science' 
> => true
>>> Science::SOLAR_MASS
> => 1.98892e+30
>>> Science.seven 
> NameError: undefined method `seven' for Science:Module
>         from (irb):3
>>>
> 
> I've also tried  rb_define_method(..."seven"...), but that didn't work 
> either.
> 
> Does anyone know how I can write module methods in C?
> 
> Thanks.

You want rb_define_module_function:

void rb_define_module_function(VALUE classmod, char *name, VALUE(*func()),
int argc);

See the Pickaxe, chapter 17, page 190.