From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2002-10-01T16:39:23+09:00 Subject: OO Perl -> Ruby 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. Phil