From: Stefano Crocco Date: 2008-02-07T00:54:41+09:00 Subject: Re: Pattern for "include"ing class methods? Alle Wednesday 06 February 2008, Clifford Heath ha scritto: > ara howard wrote: > > module M > > module ClassMethods > > end > > > > module InstanceMethods > > end > > > > def M.inherited other > > other.send :extend, ClassMethods > > other.send :include InstanceMethods > > end > > end > > Interesting idea, but it doesn't seem to work, and I don't see > how it could (though I've mangled it into something different > that does work). > > How and when should module M get used, and how does inherited > get called? > > My look at the Ruby doc says the "inherited" callback only works > on classes, not on modules...? Is this a 1.9 thing? If I change > M to a class, then of course I can't use it as the argument to > include or extend :-). > > What I *did* get to work was to add a method to class Module that > does the include and extend. Which I guess I knew I could do anyhow... > > Clifford Heath. I think you need M.included, not M.inherited: module M module ClassMethods def m1 puts "m1" end end module InstanceMethods def m2 puts "m2" end end def self.included other other.send :include, InstanceMethods other.send :extend, ClassMethods end end class C include M end C.m1 C.new.m2 Stefano Stefano