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

6

115

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

Hyperbolism answered 8/2, 2013 at 18:15 Comment(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.Nf
@IvayloStrandjev That won't help. There are too many methods to look through. It's waste of time.Belsen
@Belsen not true. Took me 30 secs. Enough for other users to be faster than me, yet quite fast.Nf
@checkit: much simpler! 8.methods.grep(/class/) You don't even need to sort with this kind of filtering :)Employment
@SergioTulentsev nice one !! it will be really helpful :)Hyperbolism
E
170
class A
end

class B < A
end

B.superclass # => A
B.superclass.name # => "A"
Employment answered 8/2, 2013 at 18:16 Comment(0)
H
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]
Heptagonal answered 5/9, 2014 at 5:26 Comment(1)
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].Outride
P
26

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.

Pea answered 12/11, 2015 at 19:41 Comment(2)
Note that base_class is only defined on ActiveRecord.Polemist
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.Pea
A
19

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

>> x = B.new
>> x.class.superclass.name
=>"A"
Argyll answered 11/7, 2014 at 20:37 Comment(0)
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].)

Autorotation answered 8/2, 2013 at 18:16 Comment(0)
P
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)

Paresthesia answered 6/6, 2017 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.