On 11/13/05, Eric Hofreiter <erichof425 / yahoo.com> wrote:
> Well, it turns out writing C extensions wasn't as easy as I had originally thought, and for only one reason: poor documentation.  Maybe I'm missing something though, does anyone know where the ruby API is documented?
>
> If not, there are two specific questions that need answering.  First, how would I retrieve a class name into a variable using rb_const_get(VALUE, ID)?  I tried something like VALUE StringClass = rb_const_get(rb_cObject, rb_intern("MyClass")), but I kept getting compile errors like "initializer is not a constant."  Second, how exactly should you use rb_define_method with an arg count of -1?  I tried making a function like func(VALUE argc, VALUE *argv, VALUE), but when I call it from ruby like func(1, 1, 1, 1), I get an arg count of 2.  It seems that the arg count is getting a count of only half the actual number arguments rounded down.  The args are still getting passed into argv, but it would be nice to be able to check the arg count without being off by one.  One final thing (I guess you could call this a third question).  What is wrong with the line rb_require("myfile")?  It says there should be a ')' before the string as well as a '{'.  There is also an '<Unkown>' error and
>  another misplaced '(' error.  Thanks a ton for any help; I really wish this API were actually documented.  At all.
>
>
> ---------------------------------
>  Yahoo! FareChase - Search multiple travel sites in one click.
>


1. "Extending Ruby" chapter of PickAxe, README.EXT and ruby.h.
2. Arg count -1 means C array. You probably need 'rb_scan_args'.
   For your case, it will look like this

  VALUE func(int argc, VALUE *argv, VALUE self)
  {
    ...
    VALUE a1, a2, a3, a4;
    rb_scan_args(argc, argv, "40", &a1, &a2, &a3, &a4);
    ...
  }

If you want more, visit RAA gonzui search page[1], or koders.com.
Learn how others use it in their sources.

[1]: http://raa.ruby-lang.org/gonzui/

--
http://nohmad.sub-port.net