From: Gaspard Bucher Date: 2009-09-15T17:30:23+09:00 Subject: Re: module to overwrite method defined via define_method I think your example does not correspond to our problem: with extend you are adding class methods to the included class and our problem was overwriting *instance* methods. In fact to avoid the "define_method" inside the class priority thing this could be a better option by using an anonymous Module: class Base def self.has_one(thing) m = Module.new do define_method(thing) do puts "Has one #{thing}" end end include m end end module Redef def dog puts "Super says:" super puts "But I say: it has nothing because I decide so." end end class Dummy < Base has_one :dog include Redef end Dummy.new.dog David Masover wrote: > On Sunday 13 September 2009 11:18:52 am Gaspard Bucher wrote: >> end >> >> Any other solution ? > > As others said, drop the 'define_method'. I'd take it a step further: > > module A > module ClassMethods > def foo > puts "'foo' from A" > end > end > def self.included(base) > base.send :extend, ClassMethods > end > end > > This is a common idiom. It's not so much load order as the fact that > class > methods don't automatically get included by "include" -- but they are > also > instance methods on the class, if that makes sense. > > The advantage of doing it this way is that you can put a lot more stuff > in > ClassMethods, or even mix in other modules (via "include") inside > ClassMethods. It's also nice and self-documenting, and it's used all > over the > place -- I believe inside Rails, at least. > > I would even go so far as to call this a best practice. Thoughts? -- Posted via http://www.ruby-forum.com/.