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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14778816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:44:20  来源:igfitidea点击:

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

ruby

提问by Rahul Tapali

Let assume I have a classes Aand Bwhere Binherits A. How do I print parent class name in B

假设我有一个 classesABwhere Binherits A。如何打印父类名称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 :)

谢谢 :)

回答by Sergio Tulentsev

class A
end

class B < A
end

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

回答by James S.

If you want the full ancestor stack try:

如果您想要完整的祖先堆栈,请尝试:

object.class.ancestors

For instance:

例如:

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

回答by wwright

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

给定一个对象(实例化类),您可以派生出父类

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

回答by Joel Fouse

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

如果 google 将任何在 Rails 中工作的人带到这里,您可能想要的是base_class,因为superclass也将遍历 ActiveRecord 继承结构。

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_classgives 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_classwill return the gem's class, not yours.

换句话说,base_class为您提供继承树的顶部,但仅限于您的应用程序的上下文。不过,公平警告,就 Rails 而言,“您的应用程序”包括您正在使用的任何 gem,因此,如果您有一个模型对 gem 中定义的某些内容进行子类化,base_class则将返回 gem 的类,而不是您的类。

回答by Chuck

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

您要查找的术语是superclass。事实上,你可以做B.superclass来获得A。(您还B.ancestors可以获取它继承自的所有类和模块的列表 - 类似于[B, A, Object, Kernel, BasicObject]。)

回答by Mukesh Kumar Gupta

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 中,一个类只能从一个其他类继承。(即一个类可以继承自一个继承自另一个类的另一个类的类,但一个类不能同时继承多个类)。BasicObject 类是 Ruby 中所有类的父类。因此,除非明确覆盖,否则它的方法可用于所有对象。

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

Ruby 通过使用 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.

您可以使用 class_name.superclass.name 进行跟踪并执行此过程,除非您在此层次结构中找到 BasicOject。BasicObject 是每个类的超类。让我们看看假设我们想看看 C 类层次树。

 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.

您可以看到类 C 的整个层次结构。请注意,使用这种方法您将找不到包含或添加到父类中的模块。

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

还有另一种方法可以找到包括模块的完整层次结构。根据 Ruby 文档祖先. 返回 mod 中包含/前置的模块列表(包括 mod 本身)。

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

Here, Mux and Kernel are Modules.

在这里,Mux 和 Kernel 是模块。

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

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