如何在 Ruby 中获得父类的类名

假设我有一个类 AB,其中 B继承 A。如何在 B中打印父类名称

class A
end


class B < A
end

有些事我已经尝试过了

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

谢谢:)

77690 次浏览
class A
end


class B < A
end


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

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

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

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

If you want the full ancestor stack try:

object.class.ancestors

For instance:

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

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.

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)