Dlaczego metoda singletonowa modułu nie jest widoczna w downstream eigenclasses, gdzie się miesza?
Rozumiem zwykłą ścieżkę wyszukiwania metody, tj.class, superclass/module, all the way up to BasicObject
. Myślałem, że to prawda także w przypadku singletonowej wersji łańcucha, ale nie wydaje się, by tak było, gdy mieszamy moduł w meta-łańcuchu. Byłbym wdzięczny, gdyby ktoś mógł wyjaśnić dlaczego w poniższym przykładzieAutomobile
modułybanner
metoda jest wywoływana zamiast jej wersji singletonowej, gdy uwzględniłem ten moduł w klasie własnej pojazdu.
module Automobile
def banner
"I am a regular method of Automobile"
end
class << self
def banner
"I am a singleton 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 singleton 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 singleton method of Car"
super
end
end
end
puts Car.banner
# I am a singleton method of Car
# I am a singleton method of Vehicle
# I am a regular method of Automobile