How do I get the parent's class name in Ruby

Ruby

Ruby Problem Overview


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 :)

Ruby Solutions


Solution 1 - Ruby

class A
end

class B < A
end

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

Solution 2 - Ruby

If you want the full ancestor stack try:

object.class.ancestors

For instance:

> a = Array.new
=> []
> a.class.ancestors
=> [Array, Enumerable, Object, Kernel, BasicObject]

Solution 3 - Ruby

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.

Solution 4 - Ruby

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

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

Solution 5 - Ruby

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].)

Solution 6 - Ruby

> 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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul TapaliView Question on Stackoverflow
Solution 1 - RubySergio TulentsevView Answer on Stackoverflow
Solution 2 - RubyJames S.View Answer on Stackoverflow
Solution 3 - RubyJoel FouseView Answer on Stackoverflow
Solution 4 - RubywwrightView Answer on Stackoverflow
Solution 5 - RubyChuckView Answer on Stackoverflow
Solution 6 - RubyMukesh Kumar GuptaView Answer on Stackoverflow