ruby列表祖先不是mixins

时间:2014-11-06 10:07:14

标签: ruby inheritance

最好用代码解释....

>> String.ancestors
=> [String, Comparable, Object, Kernel, BasicObject]
>> Comparable.ancestors
=> [Comparable]
>> Object.ancestors
=> [Object, Kernel, BasicObject]
>> Kernel.ancestors
=> [Kernel]
>> BasicObject.ancestors
=> [BasicObject]

所以我怀疑继承链是String<对象< BasicObject ...和Comparable和Kernel是mixins ...

是否有功能' foobar'哪个可以显示继承:

>> String.foobar
=> [Object, BasicObject]

如果可能的话,还包括存储单例方法的隐藏元类?

2 个答案:

答案 0 :(得分:6)

String.ancestors.grep(Class)

......................

答案 1 :(得分:4)

你可以这样做:

class Class
  def foobar
    ancestors - included_modules - [self]
  end
end