Calling singleton from C

D

Dwayne Achee

While writing a C extension to Ruby, I'm trying to call a singleton
method that resides in another class but can't figure out what
parameters to give to rb_funcall() to do this. The problem boils down
to this:

class MyClass
def MyClass.my_func
...
end
end

and then in a Ruby extension, in C, I'm trying to call MyClass.my_func
using rb_funcall(), but I can't tell what the 'receiver' and 'id'
parameters should be:

rb_funcall(VALUE receiver, ID id, int argc, ...)

Note that I'm creating another Ruby class in my C extension, *not*
extending MyClass. I checked the FAQ but couldn't find anything
close, and couldn't quite figure it out from the "Programming Ruby"
book.

Thanks for any help,
Dwayne
 
T

ts

D> class MyClass
D> def MyClass.my_func
D> ...
D> end
D> end

D> and then in a Ruby extension, in C, I'm trying to call MyClass.my_func
D> using rb_funcall(), but I can't tell what the 'receiver' and 'id'
D> parameters should be:

D> rb_funcall(VALUE receiver, ID id, int argc, ...)

The receiver is MyClass, to retrieve it (if you don't have a reference)
just do

VALUE myclass = rb_const_get(rb_cObject, rb_intern("MyClass"));

then to call the method MyClass::my_func(12) just write

rb_funcall(myclass, rb_intern("my_func"), 1, INT2NUM(12));
 

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

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top