Why is this module's initialize
method not called when it is included in Temp
class?
module Temp
def initialize
p "asdasd"
end
end
class Swap
include Temp
def initialize
p "m in class"
end
end
s = Swap.new
m in class
The Swap
class overrides the initialize
method defined in the Temp
module. When Ruby attempts to find a method it searches the inheritance hierarchy starting at the most derived class/module. In this case the search ends at the Swap class.
Overridden methods don't get called unless you explicitly call them with super
. For example
class Swap
include Temp
def initialize
p "m in class"
super
end
end
will call Temp#initialize
from Swap#initialize
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With