From: Mathieu Bouchard Date: 2005-03-26T12:02:29+09:00 Subject: Re: Instead of templates On Thu, 24 Mar 2005, Adelle Hartley wrote: > class A << MyBaseClass > def self.DoMyThing(klass=self) > ... > end > end > class B << MyBaseClass > def self.DoMyThing(klass=self) > ... > end > end > class C << B > def self.DoMyThing(klass=self) > ... > end > end > If I were writing this in C++, I'd use templates. What's the Ruby Way? I don't claim to represent the "Ruby Way", but personally I've done similar things (in the RubyX11 project) that implement parametrized-classes this way: In each parametrized class, make a (class-wide) hash, and then create new subclasses on-demand, remembering them in that hash. Then I may allow a syntax like: List.of(Float) or List[Float] ... as typenames, which get associated with an anonymous class that subclasses my baseclass. e.g.: class A @special={} def self.of(t) @special[t] ||= Class.new(self) { @mytype=t; ... } end end Where "..." may be any specialisation-specific code, e.g. an eval that recreates some methods in a speed-optimised way depending on t. Does that make sense? However I have no experience combining regular inheritance and this technique, and I know this technique may be difficult to combine with regular inheritance in a way that makes it really closer to C++. Btw, what's the class A<