开发者

Can objects made with c extensions instantiate and manage each other?

Right now I have a series of objects that are created using C extensions, so say they are

  • Foo
  • Bar
  • Baz.

I have ruby code that instantiates the three of them, calls their functions and processed the results. Let's call that:

  • Manager

Well, now Manager is my bottle开发者_开发百科neck, so I'd like to turn it into a C extension. But I don't know how to instantiate Foo, Bar, and Baz from within Manager. Do I have to use code like:

VALUE module_klass = rb_const_get(rb_cObject, rb_intern("Module"));
VALUE object_klass = rb_const_get(module_klass, rb_intern("Foo"));

And then call methods like:

rb_funcall(object_klass, rb_intern("new"), 0);     

Or is there a cleaner way?


Check out rb_class_new_instance(). You would do something like:

VALUE module_klass = rb_const_get(rb_cObject, rb_intern("Module"));
VALUE foo_klass    = rb_const_get(module_klass, rb_intern("Foo"));
VALUE foo          = rb_class_new_instance(0, NULL, foo_klass);
VALUE data         = rb_funcall(foo, rb_intern("some_foo_method"), ...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜