From: Rick DeNatale Date: 2007-03-07T06:59:02+09:00 Subject: Re: Metaprogramming and module question On 3/6/07, Phrogz wrote: > On Mar 6, 2:24 pm, "Pedro Del Gallego" > wrote: > > but i ve a proble when i try to move the pre and post method to a > > module. Can anyone explainme why the pre methods is undefined in the > > class ? > [snip] > > Including a Module into a Class only links in the 'instance' methods > of that module as instance methods of the class. Module methods (def > self.foo) are not inherited as 'class' methods. > > See http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png for a visual > explanation. > > The easiest thing to do is make those methods instance methods of the > module, and extend your Song class with them And a good way to do this automatically and make including the MetaTest module do what the OP expects is something like: module MetaTest def self.included(mod) puts "and now MetaTest has been used by #{mod.inspect}..." mod.extend(ClassMethods) end module ClassMethods def pre (name_method, &condition) old_method = instance_method(name_method) define_method(name_method) { |*args| condition.call old_method.bind(self).call(*args) } end def post (name_method, &condition) old_method = instance_method(name_method) define_method(name_method) { |*args| condition.call } end end end -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/