Can anyone explain this one:
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
end
p A.ancestors
a = A.new
a.foo
--output:--
[A, Object, Kernel]
'foo' from module
As expected, the version of foo in a's singleton class is found before
the version of foo in class A in the method lookup before.
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
--
Posted via http://www.ruby-forum.com/.