More fun with C

E

Eric Hofreiter

--0-822842872-1131850184=:77918
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Well, it turns out writing C extensions wasn't as easy as I had originall=
y thought, and for only one reason: poor documentation. Maybe I'm missin=
g something though, does anyone know where the ruby API is documented?
=20
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 =3D rb_const_get(rb_cObjec=
t, rb_intern("MyClass")), but I kept getting compile errors like "initial=
izer is not a constant." Second, how exactly should you use rb_define_me=
thod 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 ar=
e still getting passed into argv, but it would be nice to be able to chec=
k the arg count without being off by one. One final thing (I guess you c=
ould 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 t=
his API were actually documented. At all.

=09
 
D

daz

Eric said:
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: http://qurl.net/nc

.... 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
 
A

Ara.T.Howard

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.

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

kind regards.

-a
--
===============================================================================
| 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
===============================================================================
 
D

daz

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

Ehm, both ... balancin' act, y'know ;)
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

:)
 
G

Gyoung-Yoon Noh

Well, it turns out writing C extensions wasn't as easy as I had originall=
y 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 =3D rb_const_get(rb_cObject, r=
b_intern("MyClass")), but I kept getting compile errors like "initializer i=
s not a constant." Second, how exactly should you use rb_define_method wit=
h an arg count of -1? I tried making a function like func(VALUE argc, VALU=
E *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 wi=
thout being off by one. One final thing (I guess you could call this a thi=
rd question). What is wrong with the line rb_require("myfile")? It says t=
here should be a ')' before the string as well as a '{'. There is also an =
' said:
another misplaced '(' error. Thanks a ton for any help; I really wish t=
his API were actually documented. At all.

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top