More fun with C

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.

Eric Hofreiter 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?

README.EXT in the source distribution ...
It'll be on your hard drive -- else here: qurl.net - This website is for sale! - qurl Resources and Information.

.... and ...

PickAxe I
http://www.rubycentral.com/book/ext_ruby.html

I really wish this API were actually documented. At all.

Keep us informed just how bad things are, Eric.
People really appreciate it !!

daz

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/

···

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.

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

ha! don't know whether you're serious or not - but it gave me a laugh.

in any case the narray, image magick, and mmap extensions are excellent
sources of inspiration and, vicariously, documentation.

kind regards.

-a

···

On Sun, 13 Nov 2005, daz wrote:

I really wish this API were actually documented. At all.

Keep us informed just how bad things are, Eric.
People really appreciate it !!

--

ara [dot] t [dot] howard [at] gmail [dot] com
all happiness comes from the desire for others to be happy. all misery
comes from the desire for oneself to be happy.
-- bodhicaryavatara

===============================================================================

Ara.T.Howard wrote:

> Keep us informed just how bad things are, Eric.
> People really appreciate it !!

ha! don't know whether you're serious or not - but it gave me a laugh.

Ehm, both ... balancin' act, y'know :wink:

in any case the narray, image magick, and mmap extensions are excellent
sources of inspiration and, vicariously, documentation.

So you're saying that if someone is having trouble with, say,
rb_require(), they could grep existing libraries to find working
example usage and effectively ... what ... copy it ???

I'm simultaneously shocked and inspired.
Gotta go and rewrite my ethics crib-sheet ...

Feeling all tingly ...

daz

:slight_smile:

···

On Sun, 13 Nov 2005, daz wrote: