How to get parent class name in Ruby

Suppose I have classes A and B , where B inherits A How to print parent class name in B

 class A end class B < A end 

Some things I tried

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

Thank:)

+45
ruby
Feb 08 '13 at 18:15
source share
6 answers
 class A end class B < A end B.superclass # => A B.superclass.name # => "A" 
+82
Feb 08 '13 at 18:16
source share

If you want the full ancestor stack to try:

 object.class.ancestors 

For example:

 > a = Array.new => [] > a.class.ancestors => [Array, Enumerable, Object, Kernel, BasicObject] 
+39
Sep 05 '14 at 5:26
source share

When defining an object (given class), you can get the parent class

 >> x = B.new >> x.class.superclass.name =>"A" 
+14
Jul 11 '14 at 20:37
source share

In case google brings someone who works in Rails, then you may need base_class , as superclass also traverse the ActiveRecord inheritance structure.

 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 is limited by the context of your application. A fair warning, although with respect to Rails, โ€œyour applicationโ€ includes any gems that you use, so if you have a model that subclasses something defined in the gem, base_class will return the gem class, not yours.

+11
Nov 12 '15 at 19:41
source share

The term you are 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] .)

+8
Feb 08 '13 at 18:16
source share

Inheritance is the relationship between two classes. Inheritance creates parent relationships between classes. It is a mechanism for code to reuse and allow independent extensions to source software through public classes and interfaces. The advantage of inheritance is that the classes below the hierarchy receive the functions of those above, but can also add their own features.

In Ruby, a class can inherit only one class. (that is, a class can inherit from a class that inherits from another class that inherits from another class, but one class cannot inherit from many classes at once). The BasicObject class is the parent class of all classes in Ruby. Therefore, its methods are available for all objects if they are not explicitly redefined.

Ruby overcomes one-time inheritance right away with 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 track using class_name.superclass.name and execute this process if you did not find BasicOject in this hierarchy. BasicObject is a super class class for each class. suppose we want to see a tree of class hierarchy C.

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

You can see the entire hierarchy of class C. Note that using this approach, you will not find modules that are included or added to parent classes.

There is another approach for finding a complete hierarchy, including modules. According to the Ruby ancestors document. Returns a list of modules included / added to mod (including the 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)

+1
Jun 06 '17 at 12:19
source share



All Articles