114

Let assume I have a classes A and B where B inherits A. How do I print parent class name in B

class A
end

class B < A
end

Some things I have tried

>> B.new.class #=> B   #which is correct
>> B.new.parent  #=> Undefined method `parent`
>> B.parent   #=> Object
>> B.parent.class #=> Class

Thanks :)

5
  • 5
    when stuck like this always try B.methods.sort in irb. Maybe something will give you a clue on the method name you look for. Feb 8, 2013 at 18:17
  • @IvayloStrandjev That won't help. There are too many methods to look through. It's waste of time.
    – sawa
    Feb 8, 2013 at 18:20
  • 1
    @sawa not true. Took me 30 secs. Enough for other users to be faster than me, yet quite fast. Feb 8, 2013 at 20:44
  • 11
    @checkit: much simpler! 8.methods.grep(/class/) You don't even need to sort with this kind of filtering :) Feb 9, 2013 at 3:38
  • @SergioTulentsev nice one !! it will be really helpful :) Feb 9, 2013 at 3:40

6 Answers 6

170
class A
end

class B < A
end

B.superclass # => A
B.superclass.name # => "A"
0
77

If you want the full ancestor stack try:

object.class.ancestors

For instance:

> a = Array.new
=> []
> a.class.ancestors
=> [Array, Enumerable, Object, Kernel, BasicObject]
1
  • 3
    Keep in mind that also includes modules included in a class. You can see Array followed by Enumerable, which is not a parent, but a module included in Array. If you want only the classes you can do something like Array.ancestors.select { |ancestor| ancestor.is_a? Class } #=> [Array, Object, BasicObject].
    – 3limin4t0r
    May 25, 2018 at 9:38
25

In case google brings anyone here who's working in Rails, what you may want instead is base_class, as superclass will traverse the ActiveRecord inheritance structure as well.

class A < ActiveRecord::Base
end

class B < A
end

> A.superclass
=> ActiveRecord::Base
> B.superclass
=> A

> A.base_class
=> A
> B.base_class
=> A

Even further...

class C < B
end

> C.base_class
=> A

In other words, base_class gives you the top of the inheritance tree but limited to the context of your application. Fair warning though, as far as Rails is concerned "your application" includes any gems you're using, so if you have a model that subclasses something defined in a gem, base_class will return the gem's class, not yours.

2
  • 2
    Note that base_class is only defined on ActiveRecord.
    – Hosam Aly
    Sep 10, 2021 at 7:56
  • Good clarification, thank you. So it's really only for your models and won't be available for e.g. service objects, POROs, or random other things.
    – Joel Fouse
    Jun 1, 2022 at 15:53
19

Given an object (Instantiated Class) you can derive the parent Class

>> x = B.new
>> x.class.superclass.name
=>"A"
8

The term you're looking for is superclass. And indeed you can do B.superclass to get A. (You can also do B.ancestors to get a list of all the classes and modules it inherits from — something like [B, A, Object, Kernel, BasicObject].)

3

Inheritance is a relation between two classes. Inheritance create a parent child relationship between classes. It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces.The benefit of inheritance is that classes lower down the hierarchy get the features of those higher up, but can also add specific features of their own.

In Ruby, a class can only inherit from a single other class. (i.e. a class can inherit from a class that inherits from another class which inherits from another class, but a single class can not inherit from many classes at once). The BasicObject class is the parent class of all classes in Ruby. Its methods are therefore available to all objects unless explicitly overridden.

Ruby overcome the single class inheritance at once by using the mixin.

I will try to explain with an example.

module Mux
 def sam
  p "I am an module"
 end
end
class A
  include Mux
end
class B < A
end
class C < B
end
class D < A
end

You can trace by using class_name.superclass.name and do this process unless you found BasicOject in this hierarchy. BasicObject is super class o every classes. lets see suppose we want to see class C hierarchy tree.

 C.superclass
   => B
 B.superclass
  => A
 A.superclass
  => Object
 Object.superclass
  => BasicObject

You can see the whole hierarchy of class C. Point to note using this approach you will not find modules that are included or prepended in the parent classes.

There is another approach to find complete hierarchy including modules. According to Ruby doc ancestors. Returns a list of modules included/prepended in mod (including mod itself).

C.ancestors
 => [C, B, A, Mux, Object, Kernel, BasicObject]

Here, Mux and Kernel are Modules.

http://rubylearning.com/satishtalim/ruby_inheritance.html https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.