ruby 中类的类型和类的超类有什么区别?

发布于 2024-12-02 15:43:07 字数 636 浏览 5 评论 0原文

在写下我反复努力理解 ruby​​ 的对象模型后,我现在很困惑:以下是我的观察结果。

class Bird
  def speak
   puts "tweet tweet"
  end
end

>> Bird.class
=> Class
>> Class.class
=> Class
>> Class.superclass
=> Module
>> Module.class
=> Class
>> Module.superclass
=> Object
>> Object.class
=> Class
>> Object.superclass
=> nil
>> nil.class
=> NilClass
>> NilClass.class
=> Class
>> NilClass.superclass
=> Object 
and keeps going on ....

这是怎么回事?祖先 nil 或 NilClass 或 Object 或 Class 的顶点是什么? Ruby 的对象模型是如何组织的。

什么是类,什么是对象? Class 是类还是对象?对象是对象还是类?

I am but confused right now after writing my repeated efforts at understanding the object model of ruby : following are my observations.

class Bird
  def speak
   puts "tweet tweet"
  end
end

>> Bird.class
=> Class
>> Class.class
=> Class
>> Class.superclass
=> Module
>> Module.class
=> Class
>> Module.superclass
=> Object
>> Object.class
=> Class
>> Object.superclass
=> nil
>> nil.class
=> NilClass
>> NilClass.class
=> Class
>> NilClass.superclass
=> Object 
and keeps going on ....

What is going on here ? What lies at the apex of ancestry nil or NilClass or Object or Class ? How is Ruby's Object Model Organized.

What is a class and what is a object ? is Class a class or an object ? is Object an Object or a class ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不醒的梦 2024-12-09 15:43:07

在 ruby​​ 中,类对象实际上是 Class 类的实例。 class FooFoo = Class.new 几乎相同。

MyClass = Class.new
instance = MyClass.new
puts instance # => #<MyClass:0x100c14b38>

此外,class 方法更适合在实例上调用,而不是在类对象上调用。

class Foo
end

f = Foo.new
puts f.class # => Foo

调用类对象时,语义可能很奇怪。尽管superclass按照您对类对象的期望工作。

因此,考虑到所有这些,让我们一一解释一下:

>> Bird.class
=> Class

Bird 类对象有一个 Class 类,因为所有类对象都是 Class 的实例> 类。

>> Class.class
=> Class

是的,甚至 Class 也是 Class 的一个实例。在这种情况下,它实际上是一个循环引用。

>> Class.superclass
=> Module

Class 类实际上继承自 Module。毕竟,类只是一个可以实例化的模块。但所有非实例功能与模块非常相同。

>> Module.superclass
=> Object

Module 继承自Object。如果你回溯得足够远的话,就像红宝石中的一切一样。

>> Object.class
=> Class

同样,所有类对象都是Class 的实例。

>> Object.superclass
=> nil

所有 ruby​​ 的一切都以 Object 开头。它是一切的基类。因此它没有超类。

>> nil.class
=> NilClass

nil 实际上是底层 NilClass 的一个实例。 NilClass 定义 nil 响应的方法。如果需要,您甚至可以向 NilClass 添加方法。还有一个 TrueClass 和一个 FalseClass

>> NilClass.class
=> Class

同样,所有类对象都是Class 的实例。

>> NilClass.superclass
=> Object

就像任何未指定显式超类的类一样,NilClass 继承自 Object

In ruby, a class object, is actually an instance of the Class class. class Foo is nearly identical to Foo = Class.new

MyClass = Class.new
instance = MyClass.new
puts instance # => #<MyClass:0x100c14b38>

Also, the class method is more design to be called on instances, not class objects.

class Foo
end

f = Foo.new
puts f.class # => Foo

The semantics can be odd when calling on class objects. Though superclass works as you would expect on a class object.

So given all that lets explain these one by one:

>> Bird.class
=> Class

Bird the class object has a class of Class, since all class objects are instances of the Class class.

>> Class.class
=> Class

Yep even Class is an instance of Class. In this case it's actually a circular reference.

>> Class.superclass
=> Module

The Class class actually inherits from Module. After all, a class is simply a module that can be instantiated. But all non-instance functionality is pretty identical to modules.

>> Module.superclass
=> Object

Module inherits from Object. Just like everything in ruby if you go back far enough.

>> Object.class
=> Class

Again all class objects are instances of Class.

>> Object.superclass
=> nil

All ruby everything starts with Object. It is the base class for everything. Therefore it has no superclass.

>> nil.class
=> NilClass

nil is actually an instance of NilClass under the hood. NilClass defines methods that nil responds to. You can even add methods to NilClass if you want. There is also a TrueClass and a FalseClass.

>> NilClass.class
=> Class

Again all class objects are instances of Class.

>> NilClass.superclass
=> Object

NilClass inherits from Object like any class that does not specify an explicit superclass.

许你一世情深 2024-12-09 15:43:07

这有点棘手。在 Ruby 中,一切都是对象(取决于您的版本)。

  • Nil 有自己的 NilClass,因为它不允许 NilClass.new,而 Object 和 Class 允许。它是一个对象,所以你可以调用它的方法,比如 nil.nil? Object是最高的
  • Class,一切都继承自Object,甚至NilClass
  • 类也是一个Object,它知道如何用new方法从自身实例化Object。

一开始确实很奇怪,但我发现戴夫·托马斯的解释很有启发性。

请参阅:http://www.infoq.com/presentations/metaprogramming-ruby 和 < a href="http://pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming" rel="nofollow">http://pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming。后者是商业化的。

Its a bit tricky. In Ruby everything is an Object (depending on your version).

  • Nil has its own NilClass, as it does not allow NilClass.new, which Object and Class allow. It is an object so you can call methods on it like nil.nil? etc.
  • Object is the highest Class, everything inherits from Object, even NilClass
  • Class is also an Object, it knows how to instantiate Objects from itself with the new method.

It is really weird at first, but I found the explanation from Dave Thomas quite revealing.

See: http://www.infoq.com/presentations/metaprogramming-ruby and http://pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming. The later is commercial.

那请放手 2024-12-09 15:43:07

您可以从示例中得出以下结论:

  1. 示例脚本中出现的每个大写名称都指向一个类(BirdClassModule对象NilClass)。
  2. 每个类又都是 Class 类型的对象(这就是为什么 X.class 在示例中始终返回 Class 的原因)。
  3. X.superclass 返回X 的基类。
  4. Object.superclass 返回nil,因为它没有基类!这意味着 Object 的基类是 NilClass 甚至是 nil(这不是类型,而是实例)
  5. 所涉及的类的继承图如下所示:

    对象(顶级)对象对象
      | | |
    模块 Bird NilClass
      |
    班级
    

You can conclude the following from your example:

  1. Every name written in uppercase that appears in your example script points to a class (Bird, Class, Module, Object, NilClass).
  2. Every class is in turn an object of type Class (this is why X.class always returns Class in your example).
  3. X.superclass returns the base class of X.
  4. Object.superclass returns nil, because it has no base class! This does NOT mean that the base class of Object is NilClass or even nil (which is not a type, but an instance)
  5. The inheritance diagrams of the involved classes look something like:

    Object (top-level)      Object      Object
      |                       |           |
    Module                  Bird       NilClass
      |
    Class
    
め可乐爱微笑 2024-12-09 15:43:07

Class 返回对象的类(类就是对象)。超类是(派生)类的父类。

请参阅 Ruby 对象模型

Class returns a class for an object (and a class is an object). Superclass is the parent class of a (derived) class.

Please, see the Ruby Object Model.

调妓 2024-12-09 15:43:07

我认为您对实例(例如 nil)和类(例如 NilClass)之间的区别感到困惑。感到困惑是合理的,因为 Ruby 类 (NilClass) 也是实例。因此:类是一个实例。但实例不是类,除非它是一个类。

这是一个详细的层次结构,它是由 ObjectGraph

I think you're getting confused about the difference between instances (like nil) and classes (like NilClass). It's reasonable to find it confusing because Ruby classes (NilClass) are also instances. Thus: a class is an instance. An instance is not a class though unless it is a class.

Here's a detailed hierarchy, which is made by ObjectGraph.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文