From: Richard Dale Date: 2004-08-17T19:11:01+09:00 Subject: Re: Embedding Ruby as plugin Tobias Grimm wrote: > Hi! > > I have an application written in C++, that provides a plugin interface. > What I would like to do now, is to implement a plugin, that allows to > execute a ruby script, like this: > > 1) Application loads plugin.so > 2) Application invokes plugin > 3) Plugin creates object "interface" of class Interface > 4) Plugin loads rubyscript.rb > 5) rubyscript.rb uses interface object created in 3) to send messages to > the plugin > > I used SWIG to create a wrapper for the Interface class that the ruby > script should use. If I simply wanted to create an ruby extension, the > wrapper should just be compiled to a lib, that ruby can load with a > require statement. But now the wrapper is part of the plugin, which itself > is loaded by the application. How do I get the ruby script know about the > Interface class and the interface object to use? I'm not familiar with the code generated by SWIG, but you can start ruby and define a class from C like this: ruby_init(); ruby_run(); interface_module = rb_define_module("Interface"); interface_class = rb_define_class_under(interface_module, "Interface", rb_cObject); # I assume Swig generates various wrapper methods like this: rb_define_method(interface_class, "foobar", foo_bar, 1); ... rb_require("rubyscript.rb"); # Call an initialization method to create the "interface" instance rb_funcall(interface_class, rb_intern("init_rubyscript"), 0); -- Richard