From: Rick DeNatale Date: 2009-09-14T05:39:39+09:00 Subject: Re: module to overwrite method defined via define_method On Sun, Sep 13, 2009 at 2:44 PM, David A. Black wrote: > Hi -- > > On Mon, 14 Sep 2009, 7stud -- wrote: > >> But what about here: >> >> module Redef >>  def foo >>   puts "'foo' from module" >>  end >> end >> >> class A >>  define_method("foo") do >>   puts "'foo' from define_method" >>  end >> >>  def initialize >>   class << self >>     include Redef >>   end >>  end >> >>  include Redef   #<---*****CHANGE HERE >> >> end >> >> p A.ancestors >> a = A.new >> a.foo >> >> >> I would expect the order of the look up for the foo method to be: >> >> a's singleton class ==> 'foo from module' >> a's class(=A) ==> 'foo from define method' >> a's mixins(=Redef) ==> 'foo from module' >> >> >> But the output is: >> >> [A, Redef, Object, Kernel] >> 'foo' from define_method > > I believe what's happening is this: when you include Redef in a's > singleton class, Ruby sees that it's already in the ancestors (since > it's been included in A) and doesn't add it. Yep. Ruby will only include a module once in the implementation chain, it turns out it's because of the way the super keyword is implemented. I talked to Matz about why this was at RubyConf in 2007 and wrote about the conversation http://talklikeaduck.denhaven2.com/2007/11/03/a-chat-with-matz-classs-variable-reversion-and-a-mystery-explained. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale