To initialize a class from my library I’m doing this:
VALUE obj = rb_funcall(rb_path2class("MyGem::ClassName"),
rb_intern("new"), 2, arg1, arg2);
It feels like a lot of that boilerplate code could be condensed to a macro like:
VALUE obj = CLASS_INIT("MyGem::ClassName", 2, arg1, arg2)
Or is that just something I’d have to write myself if it bothered me that much, repeating funcall, path2class and intern?
It doesn’t seem to be documented in Ruby’s
README.extbut there is a function I have used in my Ruby extension calledrb_class_new_instance. The prototype is basically:It is used in
StringIO.open:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/stringio/rdoc/StringIO.html
You can see how I use it here:
https://github.com/DavidEGrayson/ruby-usb-pro/blob/master/ext/device_handle.c