Ross Bamford wrote:
> Hi,
>
> I think I must have missed something obvious, but I can't see how to
> alias a singleton method from a C extension. I think I can grab the
> singleton class, then pass that to rb_alias, but is that the right /
> best way to do it? Is there a define_singleton_alias somewhere that I've
> failed to spot?
>
> Any help would be much appreciated.

Good question.  I'm not sure why this code fails:

#include <ruby.h>

static VALUE foo_bar(){
   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_const_get(rb_cObject, rb_intern("Foo"));
   rb_define_alias(singleton, "baz", "bar");
}

When I compile and run that it fails with "undefined method `bar' for
class `Foo'" even though it clearly is defined.  I can see in class.c
that rb_define_alias is just calling rb_alias from eval.c.  It looks
like rb_alias handles singletons differently, but why it's not working
in the example I provided I'm not sure.

Hopefully Matz or Guy will pipe up.

Regards,

Dan