Ruby疯狂:类vs对象? [英] Ruby craziness: Class vs Object?

查看:100
本文介绍了Ruby疯狂:类vs对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始玩JRuby。这是我第一个红宝石的帖子。我很难理解Ruby中的类vs对象。这不意味着什么类&其他面向对象的对象。

  Class.is_a?对象

返回 true

  Object.is_a?对象


$ b b

so class&对象都是对象



这里是另一个

  Class.is_a ?类

返回 true

  Object.is_a?类


$ b b

等待,我尚未完成

  Object.instance_of? class 
Class.instance_of?类

两者都是真的

  Object.instance_of? Object 
Class.instance_of? Object

两者都为false。没有可以是对象的实例。



  。有点?类
Object.kind_of?类别

都是真的

  Class.kind_of? Object 
Object.kind_of?对象

都是真的





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

  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)之间的唯一方法差异是

 嵌套

表示继承,Is Object类似于密封类



有人可以澄清一下这是什么?



更新:到Edds注释



有趣的是,我发现

中的方法列表有很多不同之处

  c = Class.new 
print_methods(c)

&

  o = Object.new 
print_methods(o)

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

  xx = c.new // works  -  c是一个Object /是一个对象的实例c 
yy = o.new // nope - o已经是一个对象的实例,所以不能再次实例化

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

  xx.is_a?类

为false,但

  xx.is_a?对象

返回true



解决方案

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

当然,这意味着 Class 是自身的一个实例。



对象和<$ c如果你的大脑受伤, $ c> Class is_a?对象



x.is_a? y 返回 true if x.class == y或x.class< y ,即如果 x 的类为 y x 的类继承自 y 。因为每个类都继承自 x.is_a? Object 无论 x 是什么都返回true。 (在1.8反正,1.9中还有 BasicObject ,现在是继承层次结构中最基本的类)。



他们也是 is_a?类



两个对象确实是类,所以不应该感到惊讶。



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



不同于 is_a? x.instance_of ?如果 x.class == y ,而不是 x.class y 的子类。因为 x y 都是 instance_of?类,它们不是 instance_of?对象


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


< blockquote>

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



kind_of?



kind_of ?是 is_a?的别名,如上所示。


因此,两者是完全相同的,那么为什么我们有这两个。


应该指出,现在对所有类都是真的。例如。 String.is_a? Object String.is_a?类 String.instance_of?类为true, String.instance_of?对象为false,原因与上述相同。 (也由于相同的原因, String.is_a?String String.instance_of?String 都为false - String is a类,而不是字符串)。



你不能断定所有的类都是一样的。



比较方法



由于 Object Class 都是类,它们都具有由 Class 另外还有单例方法 nesting nesting 告诉你目前嵌套的模块,它与继承无关。



给定类 TheClass.methods 将返回 Class 定义的实例方法(例如 superclass ,它返回 TheClass 继承的类以及 new code> TheClass )加上该类定义的单例方法。



无论如何方法只告诉你哪些方法可以直接调用给定的对象。它不告诉你哪些方法可以在类的实例上调用。为此,您可以使用 instance_methods ,它会返回 Object code>。


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, ??

解决方案

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.

这篇关于Ruby疯狂:类vs对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆