我了解常规方法查找路径,即class, superclass/module, all the way up to BasicObject
. 我认为对于单例版本的链也是如此,但当您在元链中混合模块时似乎并非如此。如果有人能解释为什么在我将这个模块包含在 Vehicle 的 eigenclass 中时,为什么在下面的示例中调用Automobile
模块的banner
方法而不是它的单例版本,我将不胜感激。
module Automobile
def banner
"I am a regular method of Automobile"
end
class << self
def banner
"I am a class method of Automobile"
end
end
end
class Vehicle
def banner
"I am an instance method of Vehicle"
end
class << self
include Automobile
def banner
puts "I am a class method of Vehicle"
super
end
end
end
class Car < Vehicle
def banner
"I am an instance method of Car"
end
class << self
def banner
puts "I am a class method of Car"
super
end
end
end
puts Car.banner
# I am a class method of Car
# I am a class method of Vehicle
# I am a regular method of Automobile