ruby 中类的类型和类的超类有什么区别?
在写下我反复努力理解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 ruby 中,类对象实际上是
Class
类的实例。class Foo
与Foo = Class.new
几乎相同。此外,
class
方法更适合在实例上调用,而不是在类对象上调用。调用类对象时,语义可能很奇怪。尽管
superclass
按照您对类对象的期望工作。因此,考虑到所有这些,让我们一一解释一下:
Bird
类对象有一个Class
类,因为所有类对象都是Class
的实例> 类。是的,甚至
Class
也是Class
的一个实例。在这种情况下,它实际上是一个循环引用。Class
类实际上继承自Module
。毕竟,类只是一个可以实例化的模块。但所有非实例功能与模块非常相同。Module
继承自Object
。如果你回溯得足够远的话,就像红宝石中的一切一样。同样,所有类对象都是
Class
的实例。所有 ruby 的一切都以
Object
开头。它是一切的基类。因此它没有超类。nil
实际上是底层NilClass
的一个实例。NilClass
定义nil
响应的方法。如果需要,您甚至可以向NilClass
添加方法。还有一个TrueClass
和一个FalseClass
。同样,所有类对象都是
Class
的实例。就像任何未指定显式超类的类一样,
NilClass
继承自Object
。In ruby, a class object, is actually an instance of the
Class
class.class Foo
is nearly identical toFoo = Class.new
Also, the
class
method is more design to be called on instances, not class objects.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
the class object has a class ofClass
, since all class objects are instances of theClass
class.Yep even
Class
is an instance ofClass
. In this case it's actually a circular reference.The
Class
class actually inherits fromModule
. After all, a class is simply a module that can be instantiated. But all non-instance functionality is pretty identical to modules.Module
inherits fromObject
. Just like everything in ruby if you go back far enough.Again all class objects are instances of
Class
.All ruby everything starts with
Object
. It is the base class for everything. Therefore it has no superclass.nil
is actually an instance ofNilClass
under the hood.NilClass
defines methods thatnil
responds to. You can even add methods toNilClass
if you want. There is also aTrueClass
and aFalseClass
.Again all class objects are instances of
Class
.NilClass
inherits fromObject
like any class that does not specify an explicit superclass.这有点棘手。在 Ruby 中,一切都是对象(取决于您的版本)。
一开始确实很奇怪,但我发现戴夫·托马斯的解释很有启发性。
请参阅: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).
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.
您可以从示例中得出以下结论:
Bird
、Class
、Module
、对象
、NilClass
)。Class
类型的对象(这就是为什么X.class
在示例中始终返回Class
的原因)。X.superclass
返回X
的基类。Object.superclass
返回nil
,因为它没有基类!这不意味着 Object 的基类是NilClass
甚至是nil
(这不是类型,而是实例)所涉及的类的继承图如下所示:
You can conclude the following from your example:
Bird
,Class
,Module
,Object
,NilClass
).Class
(this is whyX.class
always returnsClass
in your example).X.superclass
returns the base class ofX
.Object.superclass
returnsnil
, because it has no base class! This does NOT mean that the base class of Object isNilClass
or evennil
(which is not a type, but an instance)The inheritance diagrams of the involved classes look something like:
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.
我认为您对实例(例如
nil
)和类(例如NilClass
)之间的区别感到困惑。感到困惑是合理的,因为 Ruby 类 (NilClass
) 也是实例。因此:类是一个实例。但实例不是类,除非它是一个类。这是一个详细的层次结构,它是由 ObjectGraph。
I think you're getting confused about the difference between instances (like
nil
) and classes (likeNilClass
). 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.