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. I'm not sure exactly what Cudd.Cudd_Init is doing (or returning) though. What actually happens in Cudd.Cudd_Init? David -- David Alan Black | Register for RubyConf 2002! home: dblack / candle.superlink.net | November 1-3 work: blackdav / shu.edu | Seattle, WA, USA Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com