How to get the Ancestors of an Object's Class in Ruby

Published: Friday, May 20, 2022

Greetings, friends! Sometimes, you may want to know the class hierarchy of an object in Ruby. We can use the Module#ancestors method to get the class an object and all of its ancestors in the inheritance chain.

ruby
Copied! ⭐️
class Animal
  def speak
    puts "Grrr"
  end
end

class Cat < Animal
  def speak
    puts "Meow!"
  end
end

cat = Cat.new
puts cat.class # Cat
p cat.class.ancestors # [Cat, Animal, Object, Kernel, BasicObject]
tip
You can try out this code by copying and pasting it to run.rb.

Technically, Module#ancestors is returning a list of modules included/prepended in a module, but we can use it to look at the class hierarchy of an object. In the code snippet above, we can see that our Cat class inherits from the Animal class, and the Animal class inherits from the Object class.

Lets look at integers, strings, booleans, and other types of objects. By calling the Module#ancestors method, we can see that each of these types inherit from Object.

ruby
Copied! ⭐️
puts 0.class # Integer
p 0.class.ancestors # [Integer, Numeric, Comparable, Object, Kernel, BasicObject]
puts "-------------"

puts 3.14.class # Float
p 3.14.class.ancestors # [Float, Numeric, Comparable, Object, Kernel, BasicObject]
puts "-------------"

puts "".class # String
p "".class.ancestors # [String, Comparable, Object, Kernel, BasicObject]
puts "-------------"

puts false.class # FalseClass
p false.class.ancestors # [FalseClass, Object, Kernel, BasicObject]
puts "-------------"

puts true.class # TrueClass
p true.class.ancestors # [TrueClass, Object, Kernel, BasicObject]
puts "-------------"

puts nil.class # NilClass
p nil.class.ancestors # [NilClass, Object, Kernel, BasicObject]
puts "-------------"

puts [].class # Array
p [].class.ancestors # [Array, Enumerable, Object, Kernel, BasicObject]
puts "-------------"

hash = {foo: 0, bar: 1, baz: 2}
puts hash.class # Hash
p hash.class.ancestors # [Hash, Enumerable, Object, Kernel, BasicObject]

You can also call the Module#ancestors method directly on a class instead of an instance of a class:

ruby
Copied! ⭐️
p Integer.ancestors # [Integer, Numeric, Comparable, Object, Kernel, BasicObject]
p Float.ancestors # [Float, Numeric, Comparable, Object, Kernel, BasicObject]
p String.ancestors # [String, Comparable, Object, Kernel, BasicObject]
p FalseClass.ancestors # [FalseClass, Object, Kernel, BasicObject]
p TrueClass.ancestors # [TrueClass, Object, Kernel, BasicObject]
p NilClass.ancestors # [NilClass, Object, Kernel, BasicObject]
p Array.ancestors # [Array, Enumerable, Object, Kernel, BasicObject]
p Hash.ancestors # [Hash, Enumerable, Object, Kernel, BasicObject]

class Animal
  def speak
    puts "Grrr"
  end
end

class Cat < Animal
  def speak
    puts "Meow!"
  end
end

p Animal.ancestors # [Animal, Object, Kernel, BasicObject]
p Cat.ancestors # [Cat, Animal, Object, Kernel, BasicObject]

In the previous article, we learned that we can call the Class#superclass method multiple times to investigate the class hierarchy of an object. By calling the Module#ancestors method, we can get this information much easier and quicker.

Resources