Ruby 疯狂:类与对象?

发布于 2024-10-17 11:10:48 字数 1898 浏览 1 评论 0原文

我刚刚开始玩 JRuby。这是我的第一篇红宝石帖子。我很难理解 Ruby 中的类与对象。这并不意味着像什么课程和课程那样。其他面向对象语言中的对象。例如

Class.is_a? Object

返回true 并且

Object.is_a? Object

也是

所以类&对象都是对象,

这里来了另一个

Class.is_a? Class

返回true 并且

Object.is_a? Class

也是

等等,我还没说完,

 Object.instance_of? Class
 Class.instance_of? Class

两者都是真的,

 Object.instance_of? Object
 Class.instance_of? Object

两者都是假的。是的,没有什么可以是对象的实例。

两者

 Class.kind_of? Class
 Object.kind_of? Class

都是真的

 Class.kind_of? Object
 Object.kind_of? Object

都是真的

所以两者完全相同,那为什么我们同时拥有这两个呢?

经过更多挖掘,我编写了这个简单的方法来返回两者都支持的方法列表

irb(main):054:0> def print_methods(obj)
irb(main):055:1>    obj.methods.each do |mm|
irb(main):056:2*        puts mm
irb(main):057:2>    end
irb(main):058:1> end

print_methods(Object) 和 print_methods(Class) 之间的唯一方法区别是

     Nesting

嵌套是否意味着继承,Object 是否类似于密封类?

有人可以澄清我这是什么吗?

更新: 致 Edds 评论

中的方法列表有很多差异

c=Class.new
print_methods(c)

有趣的是,我发现&

o=Object.new
print_methods(o)

。现在我明白类的实例实际上是一个类实例(并且这个类实例实际上是一个对象)而不是对象实例。甚至这个实例也允许我跨越另一个实例

  xx = c.new //works - c is an Object / and xx is a instance of an Object c
  yy = o.new //nope  - o is already a instance of an Object, so it cannot be instantiated again

所以最后,对象实际上是一个类的实例。因为

  xx.is_a? Class 

是 false,但

  xx.is_a? Object 

返回 true

我对吗??

I just started playing with JRuby. This is my first ruby post. I had a hard time understanding classes vs objects in Ruby. It doesnt mean like what classes & objects in other Object oriented laguages. for an example

Class.is_a? Object

returns true
and

Object.is_a? Object

too.

so class & Object are both objects

here comes another one

Class.is_a? Class

returns true
and

Object.is_a? Class

too.

wait, i am not done yet

 Object.instance_of? Class
 Class.instance_of? Class

Both are true

 Object.instance_of? Object
 Class.instance_of? Object

Both are false. right, nothing can be instance of object.

And

 Class.kind_of? Class
 Object.kind_of? Class

both are true

 Class.kind_of? Object
 Object.kind_of? Object

both are true

So both are exactly same, then why do we have both these.?

After some more digging, i wrote this simple method to return method list supported by both

irb(main):054:0> def print_methods(obj)
irb(main):055:1>    obj.methods.each do |mm|
irb(main):056:2*        puts mm
irb(main):057:2>    end
irb(main):058:1> end

Only method difference between print_methods(Object) and print_methods(Class) is

     Nesting

if Nesting means inheritance, Is Object similar to the sealed class??

Can someone clarify me what is all this?

Update: To Edds comment

Interestingly i see lot of differences in the method list in

c=Class.new
print_methods(c)

&

o=Object.new
print_methods(o)

Now I understand Instance of a class is really an class instance (And this class instance is actually a Object) not an object instance. And even this instance allow me to span another instances

  xx = c.new //works - c is an Object / and xx is a instance of an Object c
  yy = o.new //nope  - o is already a instance of an Object, so it cannot be instantiated again

So Finally, Object is really an instance of a Class. Because

  xx.is_a? Class 

is false, but

  xx.is_a? Object 

returns true

Am i right, ??

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

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

发布评论

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

评论(7

稀香 2024-10-24 11:10:48

基本上要理解的关键是每个类都是 Class 类的实例,每个类都是 Object 的子类(在 1.8 - 1.9 中每个类都是子类) BasicObject)。因此,每个类都是一个对象,因为它是 Object 子类(即 Class)的实例。

当然,这意味着 Class 是其自身的实例。如果这让你的大脑受伤,那就别想得太深了。

ObjectClassis_a?对象

x.is_a?如果 x.class == y 或 x.class y 返回 true y,即如果x的类是y或者x的类继承自y 。由于每个类都继承自对象 x.is_a?无论x 是什么,Object 都会返回 true。 (无论如何,在 1.8 中,在 1.9 中还有 BasicObject,它现在是继承层次结构中最基本的类)。

他们也是is_a? Class

ObjectClass 确实都是类,所以这并不奇怪。

它们也是instance_of?类,但不是instance_of?对象

is_a? 不同,x.instance_of? y 仅在 x.class == y 时返回 true,而在 x.classy 的子类时则不返回 true。那么既然xy都是instance_of?类,它们不是instance_of?对象

对,没有任何东西可以是对象的实例。

那不是真的。 Object.new.instance_of?对象 为真。

有点儿?

kind_of?is_a? 的别名,因此请参见上文。

所以两者完全相同,那为什么我们要有这两个。?

应该指出的是,到目前为止的一切对于所有类别都是正确的。例如String.is_a?对象String.is_a?类String.instance_of? Class 为 true 且 String.instance_of?出于与上述相同的原因,Object 为 false。 (出于同样的原因,String.is_a? StringString.instance_of? String 都为 false - String 是一个类,而不是字符串)。

您不能由此得出所有类都是相同的结论。它们只是同一类的所有实例。

比较方法

由于 ObjectClass 都是类,因此它们都具有 Class 定义的所有实例方法。 Class 另外还有单例方法嵌套nesting 告诉您当前嵌套在哪个模块中,它与继承无关。

对于任何给定的类,TheClass.methods将返回由Class定义的实例方法(例如superclass,它返回TheClass< /code> 继承自 new,它创建 TheClass 的新实例)以及该类定义的单例方法。

无论如何,methods 只告诉您可以在给定对象上直接调用哪些方法。它不会告诉您可以在类的实例上调用哪些方法。为此,您可以使用 instance_methods,它为 ObjectClass 返回显着不同的结果。

Basically the key thing to understand is that every class is an instance of the Class class and every class is a subclass of Object (in 1.8 - in 1.9 every class is a subclass of BasicObject). So every class is an object in the sense that it is an instance of a subclass of Object, i.e. Class.

Of course this means that Class is an instance of itself. If that makes your brain hurt, just don't think about it too deeply.

Object and Class are is_a? Object

x.is_a? y returns true if x.class == y or x.class < y, i.e. if x's class is y or x's class inherits from y. Since every class inherits from object x.is_a? Object returns true no matter what x is. (In 1.8 anyway, in 1.9 there's also BasicObject which is now the most basic class in the inheritance hierarchy).

They are also is_a? Class

Both Object and Class are indeed classes, so that should not be surprising.

They are also instance_of? Class, but not instance_of? Object.

Unlike is_a?, x.instance_of? y only returns true if x.class == y, not if x.class is a subclass of y. So since both x and y are instance_of? Class, they're not instance_of? Object.

right, nothing can be instance of object.

That's not true. Object.new.instance_of? Object is true.

kind_of?

kind_of? is an alias for is_a?, so see above.

So both are exactly same, then why do we have both these.?

It should be pointed out that everything up to now is true for all classes. E.g. String.is_a? Object, String.is_a? Class and String.instance_of? Class are true and String.instance_of? Object is false for the same reasons as above. (Also String.is_a? String and String.instance_of? String are both false for the same reasons - String is a class, not a string).

You can not conclude from this that all the classes are the same. They're just all instances of the same class.

Comparing methods

Since both Object and Class are classes, they both have all the instance methods defined by Class. Class additionally has the singleton method nesting. nesting tells you which module you're currently nested in, it has nothing to do with inheritance.

For any given class TheClass.methods will return the instance methods defined by Class (e.g. superclass, which returns the class which TheClass inherits from, and new which creates a new instance of TheClass) plus the singleton methods defined by that class.

Anyway methods only tells you which methods can be called directly on a given object. It does not tell you which methods can be called on an instance of a class. For that you can use instance_methods, which returns significantly different results for Object and Class.

趴在窗边数星星i 2024-10-24 11:10:48

在 Ruby 中,一切都是对象,包括类和模块。 Object 是最低级的类(好吧,在 Ruby 1.9.2 中也有 BasicObject 但这是另一个故事了)。

请参阅以下输出。

> Object.ancestors
# => [Object, Kernel, BasicObject] 
> Class.ancestors
# => [Class, Module, Object, Kernel, BasicObject] 
> Module.ancestors
# => [Module, Object, Kernel, BasicObject] 
> String.ancestors
# => [String, Comparable, Object, Kernel, BasicObject]

正如您所看到的,ClassModule 都继承自 Object

回到你最初的断言,你必须理解

  • is_a?
  • kind_of'
  • instance_of?

之间的区别,它们是不可互换的。如果 other 是同一类或祖先,则 is_a?kind_of? 返回 true。相反,仅当 other 是同一类时,instance_of? 才返回 true。

> Class.is_a? Object
# => true 
> Class.kind_of? Object
# => true 
> Class.instance_of? Object
# => false 

In Ruby, everything is an Object including classes and modules. Object is the most low-level class (well, in Ruby 1.9.2 there is also BasicObject but this is an other story).

See the following output.

> Object.ancestors
# => [Object, Kernel, BasicObject] 
> Class.ancestors
# => [Class, Module, Object, Kernel, BasicObject] 
> Module.ancestors
# => [Module, Object, Kernel, BasicObject] 
> String.ancestors
# => [String, Comparable, Object, Kernel, BasicObject]

As you can see, both Class and Module inherits from Object.

Back to your original assertions, you have to understand the difference bewteen

  • is_a?
  • kind_of'
  • instance_of?

They are not interchangeable. is_a? and kind_of? returns true if other is the same class or an ancestor. Conversely, instance_of? returns true only if other is the same class.

> Class.is_a? Object
# => true 
> Class.kind_of? Object
# => true 
> Class.instance_of? Object
# => false 
记忆里有你的影子 2024-10-24 11:10:48

其中一个答案提到了这一点:

基本上要理解的关键是每个类都是一个
Class 类的实例,每个类都是 Object 的子类。
所以每个类都是一个对象,因为它是一个实例
Object的子类,即Class。

我只是想为那些头脑有点扭曲的人换个说法。首先问自己:编程中的实例是什么?编程中的子类是什么?实例只是蓝图(类)的实现变体。子类只是一个从另一个类(蓝图)继承的类(蓝图)。所以当你创建一个新的类时:

class Apple
end

Apple是Class的一个实例,也就是说,它是蓝图的一个已实现的变体。它采用蓝图并用自己的变体填充细节(方法和变量)。嗯,这个蓝图继承了另一个蓝图,那就是Object。所以每个类都是Class的一个实例,而Class又是Object的子类。

class A 
end
A.superclass  
=> Object     
A.class  
=> Class

注意类在其继承链中有模块(模块作为混合包含在类中,也许是因为类的父级是对象?)。

A.is_a?(Module)
 => true  

类 A 的实例 (A.new) 将具有它们自己的 A 的实现变体。但它们是对象实例。所以我们必须区分类实例(例如类A end)和对象实例(a = A.new)。对象实例具有不同的继承链。它们是类实例蓝图的已实现变体,而不是类 Class 的变体。

这意味着它们的继承链中没有类或模块。而是其他对象实例,因此如果 A 有对象实例,B 有对象实例,并且 A 继承自 B,那么当我们实例化 A 的新对象实例时,该实例的继承链中将会有 B 实例。

它们也将从 Object 继承,因为 Ruby 中的所有内容都继承自 Object。

a = A.new
 => #<A:0x007f966449b8d8> 
a.is_a?(Class)
 => false 
a.is_a?(Module)
 => false 
a.is_a?(Object)
 => true 

这是思考这一切的最好方式。不要让你的想法走得太深。接受我写的这个。

One of the answers mentions this:

Basically the key thing to understand is that every class is an
instance of the Class class and every class is a subclass of Object.
So every class is an object in the sense that it is an instance of a
subclass of Object, i.e. Class.

I just want to word it differently for those who have a little brain twist. First ask yourself: What is an instance in programming? And what is a subclass in programming? An instance is just a realized variation of a blueprint (the Class). A subclass is simply a class (blueprint) that inherits from another class (blueprint). So when you create a new class:

class Apple
end

Apple is an instance of Class, that is, it is a realized variation of the blueprint. It takes the blueprint and fills in the details (methods and variables) with its own variation. Well, the blueprint inherits from another blueprint, which is Object. So every class is an instance of Class, which is a subclass of Object.

class A 
end
A.superclass  
=> Object     
A.class  
=> Class

Note Class has Module in its inheritance chain (Module included in Class as a mixin perhaps since Class's parent is Object?).

A.is_a?(Module)
 => true  

Instances (A.new) of class A will have their own realized variations of A. But they are object instances. So we must distinguish class instances (e.g. class A end) and object instances ( a = A.new). Object instances have a different inheritance chain. They are a realized variation of a class instance blueprint, not a variation of class Class.

This means in their inheritance chain is not Class or Module. But rather other object instances, so if A has object instances and B has object instances and A inherits from B, when we instantiate a new object instance of A, this instance will have B instances in its inheritance chain.

They will also inherit from Object, since everything in Ruby inherits from Object.

a = A.new
 => #<A:0x007f966449b8d8> 
a.is_a?(Class)
 => false 
a.is_a?(Module)
 => false 
a.is_a?(Object)
 => true 

And this is the best way to think about it all. Do not go too deep with your thinking. Accept this as I have written it.

把梦留给海 2024-10-24 11:10:48

Ramesh,在 ruby​​ 中一切都是对象,类也不例外。

在这种情况下,在 irb 中尝试这个

ruby-1.9.2-p136 :001 > o = Object.new
=> #<Object:0x000001020114b0> 
ruby-1.9.2-p136 :002 > o.is_a? Class
=> false 
ruby-1.9.2-p136 :003 > o.is_a? Object
=> true 

,我创建了一个对象的实例,并检查它是一个类(假)还是一个对象(真)。

ruby 中的类是某种用于创建该类实例的模板对象。抱歉,这不是很清楚。关键概念是 Ruby 是一种纯粹的面向对象语言,与 Java 不同。

Ramesh, in ruby everything is an object, and a Class is no exception.

try this in irb

ruby-1.9.2-p136 :001 > o = Object.new
=> #<Object:0x000001020114b0> 
ruby-1.9.2-p136 :002 > o.is_a? Class
=> false 
ruby-1.9.2-p136 :003 > o.is_a? Object
=> true 

in this case, I've created an instance of an Object, and checked if it's a class (false) or a Object (true).

A Class in ruby, is some kind of template object used to create instances of that class. Sorry that this is not super clear. The key concept is that ruby is a pure object oriented language, as opposed to Java.

相对绾红妆 2024-10-24 11:10:48

类/元类层次结构总是有点令人费解:)仅供比较,这是 Smalltalk 中的;在 Ruby 中,设置基于相同的原则,只是它没有 BehaviorClassDescription 区别,并且需要考虑模块和特征类。

Smalltalk 对象模型的完整解释可在 Pharo by Example 中找到,如 相关问题

The class/metaclass hierarchy is always a little puzzling :) Just for comparison, here's the one in Smalltalk; in Ruby, the setup is based on the same principles, except it doesn't have the Behavior and ClassDescription distinctions, and there are modules and eigenclasses to take into account.

A full explanation of the Smalltalk object model is available in Pharo by Example, as pointed by this related question.

抚你发端 2024-10-24 11:10:48

正如 _why 在这篇文章中所写的那样

对象不存储方法,只有类可以。

前几节有一些关于类与对象的优点

As _why writes in this article

objects do not store methods, only classes can.

The first couple sections have some good points about classes vs objects

秋千易 2024-10-24 11:10:48

将类视为全局对象。

Think of Classes as global objects.

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