Ruby/Reflection/ancestors

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Get ancestors

p String.ancestors          # => [String, Enumerable, Comparable, Object, Kernel]
p Array.ancestors           # => [Array, Enumerable, Object, Kernel]
class MyArray < Array
end
p MyArray.ancestors         # => [MyArray, Array, Enumerable, Object, Kernel]
p Object.ancestors          # => [Object, Kernel]
class MyClass
end
p MyClass.ancestors         # => [MyClass, Object, Kernel]



Look at an object"s ancestors with Module"s ancestors method, which returns a list of both classes and modules

myString = "asdf"
p myString.class.ancestors # => [String, Enumerable, Comparable, Object, Kernel]
p String.ancestors # => [String, Enumerable, Comparable, Object, Kernel]