2
我一直在浏览关于如何将Ruby嵌入到C++程序中的教程。我已经发现如何通过“rb_define_class”和“rb_define_class_under”以及通过“rb_define_method”定义一个类。现在我需要一个很好的例子来解释如何用C++编写的ruby类来包装现有的C++对象(指针)。例如:在Ruby和C++之间交换数据
class MyClass
{
public:
MyClass();
void MyMethod();
};
VALUE myclass_init(VALUE self)
{
// I'd like to create a new MyClass instance and store its pointer inside "self"
}
VALUE myclass_meth(VALUE self)
{
// Now i need to retrieve the pointer to the object and call its method
}
int main(int argc, char* argv[])
{
ruby_init();
ruby_init_loadpath();
VALUE myclass = rb_define_class("MyWrapperClass", rb_cObject);
rb_define_method(myclass, "initialize", (VALUE(*)(...))myclass_init, 0);
rb_define_method(myclass, "myWrappedMethod", (VALUE(*)(...))myclass_meth, 0);
// Loading ruby script skipped..
ruby_finalize();
return 0;
}
我还需要一种方法来处理垃圾收集,以释放我包装的对象(和其他东西)。对不起,英文不好,谢谢谁会试图回答这个问题!
你见过:http://media.pragprog.com/titles/ruby3/ext_ruby.pdf ?您可能想看看我的示例gem:https://github.com/neilslater/ruby_nex_cpp(这是C++的本地扩展,不是嵌入式Ruby,但大部分API集成都是相同的)。另外看看RICE,它有更完整的图书馆。 –
谢谢!这正是我一直在寻找的! – napco
另请参阅我的答案 - 我将它放到代码的上下文中。 –