Rb_const_get documentation

Lähettäjä: Brian Palmer <brian@pocketmartiansoftware.com>
Aihe: rb_const_get documentation

What's the best place to find out more about the rb_const_get family of
functions for writing C extensions? I'm writing my first C library for
Ruby, and struggling with how to get VALUEs for Classes and Modules
provided by Ruby and other extensions. The Pickaxe 2.0 has a wonderful
section on C extensions, for the most part, but it seems to omit mention
of rb_const_get completely. Took me an hour of searching the ml to even
discover its existence.

A few pressing questions:
(1) What does the rb_cObject parameter that I'm always passing in do?
What other things might I pass in for this argument?
(2) How do I get a handle to a Class that's inside a module? I tried
calling (for example) rb_const_get(rc_cObject, rb_intern("GLIT::Vec"))
but I get an error. However, calling GLIT::Vec.new from a ruby script
that's using the C extension works fine, so I must have the syntax wrong.

The signature is

VALUE rb_const_get (VALUE klass, ID id)

so the first parameter is the class in which to search for the
constant (the second is a Symbol). For top-level, the class is Object
(or rb_cObject). For a nested class, you should look in the enclosing
class. So, you'd do

VALUE cGlit = rb_const_get(rb_cObject, rb_intern("GLIT"));
VALUE cGlitVec = rb_const_get(cGlit, rb_intern("Vec"));

I think :slight_smile:

Thanks a bundle for any help you all can provide. I appreciate it.

-- Brian Palmer

E

Perfect! Thanks for the tip.

-- Brian

E S wrote:

···

Lähettäjä: Brian Palmer <brian@pocketmartiansoftware.com>
Aihe: rb_const_get documentation

What's the best place to find out more about the rb_const_get family of functions for writing C extensions? I'm writing my first C library for Ruby, and struggling with how to get VALUEs for Classes and Modules provided by Ruby and other extensions. The Pickaxe 2.0 has a wonderful section on C extensions, for the most part, but it seems to omit mention of rb_const_get completely. Took me an hour of searching the ml to even discover its existence.

A few pressing questions:
(1) What does the rb_cObject parameter that I'm always passing in do? What other things might I pass in for this argument?
(2) How do I get a handle to a Class that's inside a module? I tried calling (for example) rb_const_get(rc_cObject, rb_intern("GLIT::Vec")) but I get an error. However, calling GLIT::Vec.new from a ruby script that's using the C extension works fine, so I must have the syntax wrong.
   
The signature is

VALUE rb_const_get (VALUE klass, ID id)

so the first parameter is the class in which to search for the constant (the second is a Symbol). For top-level, the class is Object (or rb_cObject). For a nested class, you should look in the enclosing class. So, you'd do

VALUE cGlit = rb_const_get(rb_cObject, rb_intern("GLIT"));
VALUE cGlitVec = rb_const_get(cGlit, rb_intern("Vec"));

I think :slight_smile:

Thanks a bundle for any help you all can provide. I appreciate it.

-- Brian Palmer
   
E

(...)

>VALUE cGlit = rb_const_get(rb_cObject, rb_intern("GLIT"));
>VALUE cGlitVec = rb_const_get(cGlit, rb_intern("Vec"));
>

(...)

also you can go:
VALUE cGlitVec = rb_path2class("GLIT::Vec");

On a side note it would be nice if all the functions, structs, typdefs,
macros and variables ruby.h and intern.h were documented. I would be
willing to take part in an effort to do this. Anyone else?
Probably need to start out by figuring out the best way of going about
it. A format would have to be decided on and also a way to cross
reference/search/interact with the documentation (so people with
questions about resolving constants would be able to find answers).
Seems like there would need to be a central place to put the
documentation. SVN repository, wiki, ...?

I played around with using Doxygen for doing this, but I think it would
be better if the documentation was external to the .h files - mainly
because I don't think Matz and others would like having pages and pages
of docs embedded in these files and because keeping the files with
embedded docs in sync with the original .h files would be a nightmare.
On the other hand a format like RDoc or Doxygen is good because there
isn't a huge amount of mark up to learn... well, I guess other people
will have some suggestions for this...

-Charlie