From: Robert Klemme Date: 2008-03-17T06:30:22+09:00 Subject: Re: Parametric module or injecting code via class method? On 14.03.2008 00:42, ara howard wrote: > On Mar 13, 2008, at 4:20 PM, Robert Klemme wrote: > >> Using a Struct generated class as base class is a bad example IMHO >> because those classes do already have the equation properties. > > heh - yeah bad example. i just used struct to avoid defining atts 'a' > and 'b' and an initializer - i think the example stands with that > though. > >> At the moment the only advantage I see in using modules is avoidance >> of namespace pollution. Personally I'd just have a method in class >> Module equate_on which defines methods as shown. > > > well namspace pollution is no small thing! the other huge advantage > is that running rdoc over something like > > class C > module ClassMethods > end > end > > is about a billion times better than running it over > > class C > singleton_class.module_eval .... > > not to mention grep et all. The alternative - at least as far as I see it - is class Module def equate_on(fields) ... end end class AnyClass attr_accessor :foo, :bar equate_on :foo end > the other huge advantage of using modules is subtle: if you start out > with code such as > > module M > > def self.included other > add_equate_method_to other > end > > end > > and, over the course of your project end up with > > module M > X = 4 > Y = 2 > end > > > then the result of > > class C > include M > end > > is that X and Y are dumped into C *even though the included block was > purposely setup to define what happens logically for module > inclusion*. by carving out the target of inclusion one can do > > module M > module Methods > end > > def self.included other > other.extend Methods > end > end > > and dump constants into M to your heart's content without > inadvertently dumping them into every class that wanted a little class > method. > > this last bit i just recently have been using - but born out from long > debugging session ;-) Yeah, but you can achieve the functionality without modules at all - at the small cost of defining a single instance method in class Module. I guess you have some other code in mind (at least the one that you had to debug) but I am still not convinced that using modules is the best approach for such a basic thing as defining equivalence between instances of a class. My .02 EUR... Kind regards robert