module M
def meth
p "M"
end
end
class C
def meth
p "C"
end
end
class D < C
#include M
end
d = D.new
p d.meth
Hi, can you please explain why the result of this code in Ruby is:
"C"
"C"
(TWICE, as you can see)?
Also, why uncommenting the line in class D
definition renders "M"
, also TWICE?
This is about calling method x in a class which inherits from another class with method x and mixes in a module which also contains method x.
I can guess the double display of "M"
in the second case is owed to the call d.meth
: meth
is found in the superclass of D
and also in the module
that D
includes, so maybe the module method meth overwrites class C
's method meth
. And then, maybe both are executed.