class Bird
def self.bird_ancestors
ancestors.first(ancestors.find_index(Bird)+1)
end
end
class Duck < Bird
end
class FeatheredDuck < Duck
end
FeatheredDuck.bird_ancestors => [FeatheredDuck,Duck,Bird]
Duck.bird_ancestors => [Duck,Bird]
Bird.bird_ancestors => [Bird]
如何可以引用內Bird
的Bird
無需它是明確的?我知道self
和__class__ doesnt
的工作。
您的類定義的排序是錯誤的。 – Agis
另外,你的意思是「'self'不起作用」?在類方法中,'self'指向當前類。 – Agis
另外,你似乎在呈現如此多與你的問題無關的事情。 – sawa