Tim Pease wrote:
> >
> > void Init_foo(){
> > VALUE cFoo, singleton;
> >
> > cFoo = rb_define_class("Foo", rb_cObject);
> >
> > rb_define_singleton_method(cFoo, "bar", foo_bar, 0);
> >
> > singleton = rb_const_get(rb_cObject, rb_intern("Foo"));
> > rb_define_alias(singleton, "baz", "bar");
> > }
> >
Your line of code above that grabs the singleton class is incorrect.
singleton = rb_const_get( rb_cObject, rb_intern("Foo"));
This will just return the class object for your "Foo" class. In this
case singleton is equivalent to cFoo. Try this out in your code ...
singleton == cFoo
That should equate to true. Here is the correct way to grab the
singleton class from C code ...
cat foo.c
#include <ruby.h>
static VALUE
foo_bar( VALUE self ) {
return rb_str_new2( "hello" );
}
void
Init_foo( ) {
VALUE cFoo, singleton;
cFoo = rb_define_class("Foo", rb_cObject);
rb_define_singleton_method(cFoo, "bar", foo_bar, 0);
singleton = rb_singleton_class(cFoo);
rb_define_alias(singleton, "baz", "bar");
}
Now you can do this ...
Foo.bar #=> "hello"
Foo.baz #=> "hello"
The magic syntax is the line ...
singleton = rb_singleton_class(cFoo);
That gives me Foo's singleton.
Blessings,
TwP
···
On 10/30/06, Daniel Berger <djberg96@gmail.com> wrote:
> On 10/30/06, Daniel Berger <djberg96@gmail.com> wrote: