From: Lyle Johnson Date: 2001-07-17T03:54:27+09:00 Subject: [ruby-talk:17944] Re: RUBY C++ Extension > Does anybody have the source to a WORKING C++ Class exported to RUBY > (preferably using MSVC 6.0) ? The source code for FXRuby, obviously, has a bunch of these. You can download its source code from the home page: http://fxruby.sourceforge.net You might also look at Ruby/Qt or some of the other C++-based extension modules in the Ruby Application Archive. > I would like to instantiate my C++ classes in RUBY just the way as in > C++ code. > All samples and docs I found are on straigt C Extensions and that does > not help much. Have you taken a look at the "Extending Ruby" chapter in "Programming Ruby", by Thomas and Hunt? They have a nice example of how to wrap-up a C struct and its "member functions"; it shouldn't be much of a logical leap to extend this example to work with a C++ class. The book is available online, BTW, at: http://www.rubycentral.com/book > Also how can I export the overloaded operators (for addition etc..) These are just instance methods for the class. For example, addition is an instance method named "+". I don't know if the names of these methods are explicitly documented anywhere, but you can look in the Ruby source code for examples of how to implement such methods (see, e.g. "numeric.c", where Ruby's built-in Fixnum and Float classes are implemented). > And very important can i mirror an inheritance relationship in RUBY ? > say i have a Ruby Method > world_add( Object ) which takes an object.. Object is a C++ Class. Now > i have a C++ class which is derived from Object. How do i export it to > Ruby so that the world_add method treats it just like the Object base > class ? This is pretty important to me. This works exactly as you would expect. The Ruby API provides a function rb_define_class_under() to create new Ruby classes, and the last argument of this function is the base class of the class (or rb_cObject, if it's a "top-level" class). > Also my progam consists of more than one DLL in the main DLL i would > like to export the Object Base Class to ruby and in some plugin DLLs i > would like to export only classes derived from Object which can then > be added to a world with world_add ? DOes that work ? Over the DLL > boundary ? What to do to make it work? You *should* be able to make this work, but it might be a little tricky. I haven't tried this, but here's the approach I think I would take: In the plug-in DLL, you would need to get a Ruby VALUE containing a reference to the "Class" object for your "Object" base class. This is required because, as we saw earlier, you need to pass a reference to the Ruby base class into rb_define_class_under(). When you define a Ruby class it creates a module-level constant whose name is the class name; that constant is a reference to the associated Class object. So you'd write something like: VALUE cMyBaseClass = rb_const_get(mMyModule, "MyBaseClass"); VALUE cMyDerivedClass = rb_define_class_under(mMyModule, "MyDerivedClass", cMyBaseClass); Hope this helps, Lyle