From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2002-10-02T02:20:35+09:00 Subject: Re: OO Perl -> Ruby In article , wrote: >Hi -- > >On Tue, 1 Oct 2002, Phil Tomson wrote: > >> I'm wrapping a C library for a Binary Decision Diagram package using swig. >> The creator of the C library also created an extension for Perl using xs. >> I'd kind of like to use a similar interface as their OO Perl extension. >> >> Now in Perl you can 'bless' a reference so that it becomse an object. >> Here's what they did in their 'new' function: >> >> package Cudd; >> sub new { >> my $class = shift; >> my $numVars = shift || 0; >> my $numVarsZ = shift || 0; >> my $numSlots = shift || &Cudd::UNIQUE_SLOTS; >> my $cacheSize = shift || &Cudd::CACHE_SLOTS; >> my $maxMemory = shift || 0; >> my $manager = >> Cudd_Init($numVars,$numVarsZ,$numSlots,$cacheSize,$maxMemory); >> bless \$manager, $class; >> return \$manager; >> } >> >> Now, I'm translating this into Ruby like: >> >> class CuddClass >> include Cudd >> def initialize(numVars=nil,numVarsZ=nil,numSlots=nil,cacheSlots=nil, >> maxMemory=nil) >> @numVars = numVars || 0 >> @numVarsZ= numVarsZ || 0 >> @numSlots= numSlots || Cudd::CUDD_UNIQUE_SLOTS >> @cacheSlots= cacheSlots || Cudd::CUDD_CACHE_SLOTS >> @maxMemory= maxMemory || 0 >> #Cudd.Cudd_Init is defined on the C side: >> @manager= >> Cudd.Cudd_Init(@numVars,@numVarsZ,@numSlots,@cacheSlots,@maxMemory) >> end >> end >> >> Now, in Perl, the $manager that gets returned by the Cudd_Init function >> gets blessed and becomes the reference to the object - the $manager >> reference can respond to a certain set of method calls on it's own. >> Blesssing $manager into the Cudd package also adds some additional >> methods that are defined in the Cudd package. In Ruby, calling: >> CuddClass.new will call CuddClass' initialize and return a reference to an >> instance of CuddClass which is different from what's happening in Perl. So >> the question is, what's the best way to translate this into Ruby? I'm >> thinking that I could use method_missing to redirect unknown (or at >> least of unknown to CuddClass, but known to @manager) method calls to the >> @manager reference. > >It seems like there are two initializations going on here, and it >would probably be good to try to combine them so that you have one >object that does everything you need. Things that come to mind are >delegation, and possibly 'extend'ing the object with a module. Actually, as it turns out I was making it more complicated than I needed to (it was about 1AM, after all :). Turns out there is another class called BDD and objects of that class get created in various methods of CuddClass (Which I've now renamed to CuddMgr, btw). Now on the Perl side they pass the $manager into the BDD constructor, for Ruby all I have to do is pass the @manager into the BDD constructor. If anyone is interested I can post the code snippet later, I'm on a different machine now. > >I'm not sure exactly what Cudd.Cudd_Init is doing (or returning) >though. What actually happens in Cudd.Cudd_Init? Cudd_Init returns another object - in this case it's an object of type DdManager which is defined in a struct on the C side (Swig very nicely turns structs into classes for you). Basically I'm turning a non-OO C API into an object oriented one - they did the same with the Perl extension. Phil