简体   繁体   English

关于Ruby中OBJECTS的问题

[英]Questions about OBJECTS in Ruby

I'm reading 'metaprogramming in ruby' 我正在阅读'红宝石中的元编程'

its such an EXCELLENT book. 这是一本优秀的书。 Seriously, it talks about stuff that I never hear mentioned elsewhere. 说真的,它谈到了我从未听过其他地方提到过的东西。

I have a few specific questions however about objects (I'm in the first few chapters) 我有一些关于对象的具体问题(我在前几章)

  1. I understand that the RubyGems gem installs the method 'gem' to the module Kernel so that it shows up on every object. 据我所知,RubyGems gem将方法'gem'安装到模块Kernel,以便它显示在每个对象上。 Is there a reason they didnt put it into the Object class? 有没有理由他们没有把它放入Object类?

  2. He talks about how when ruby looks for the method it always goes right then up. 他谈到了当红宝石在寻找方法的时候,它一直在向上发展。 What exactly does 'up' mean? 'up'究竟是什么意思? I see it in the diagram, its just that I dont really understand the purpose of 'up'. 我在图中看到它,它只是我不明白'up'的目的。 he doesnt explain that part much. 他没有解释那部分内容。

  3. What is the point of the Object class? Object类有什么意义? How come those methods cant be just placed into Class? 为什么这些方法不能被放入课堂? If every object belongs to a class (even if its Class), then what is the point of object, basicobject, and kernel? 如果每个对象都属于一个类(即使它的类),那么对象,基本对象和内核的重点是什么?

  4. String, Array, blah blah are obviously an instance of Class. String,Array,blah blah显然是Class的一个实例。 Class is also an instance of itself. 类也是它自己的一个实例。 So if Class is an instance of Class.... how does it also inherit from Object? 所以如果Class是Class的一个实例......它是如何继承Object的呢? Where in the code does it relates to BOTH Class and Object? 代码中的哪个位置与BOTH类和对象有关?

  5. I know kernel contains methods such as puts that can be used everywhere, and this relates to question 1, but why cant they just condense it and put it all into Object... where it would seem everything inherits from object anyway 我知道内核包含可以在任何地方使用的put等方法,这与问题1有关,但是为什么它们只是压缩它并将它全部放入Object ...它似乎一切都从对象继承而来

  1. Both would work, but typically methods on Object should only be methods that deal with a particular object. 两者都可以工作,但通常Object上的方法应该只是处理特定对象的方法。 Puting things in the Kernel module are less about about object and more global. Kernel模块中的内容不是关于对象而是更全局的。

  2. I assume it means "up the inheritance chain". 我认为这意味着“继承链”。 So it looks for the method on the child class, then on that classes parent class until it finds one or runs out of parent classes. 因此,它在子类上查找该方法,然后在该类上查找父类,直到找到一个或用完父类。

  3. Object is the base class of all objects, naturally (For ruby 1.8 at least) . Object自然是所有对象的基类(至少对于ruby 1.8) The crazy part is that a class is actually an instance of the Class class. 疯狂的部分是一个类实际上是Class类的一个实例。 (you follow that?) So adding instance methods to Class would add methods to class objects, but not instances of those classes. (您遵循这个?)因此,向Class添加实例方法会向类对象添加方法,但不会向这些类添加实例。

  4. Nearly everything in ruby is an object. 红宝石中几乎所有东西都是物体。 Class.superclass is actually Module (which is like a class you can't instantiate) and Module.superclass returns Object . Class.superclass实际上是Module (它就像一个你无法实例化的类), Module.superclass返回Object So Class < Module < Object is the inheritance chain if the Class class. 所以Class < Module < ObjectClass类的继承链。 (For ruby 1.8 at least) (至少为红宝石1.8)

  5. More convention than anything. 更多的约定比什么。 Since Object can get rather HUGE, it's customary to put things into modules and then combine those modules later. 由于Object可能会变得相当巨大,因此习惯上将事物放入模块中,然后再将这些模块组合起来。 If the method doesn't deal with an instance of an object directly as self then the method doesn't belong directly in Object . 如果该方法不直接将对象的实例作为self则该方法不直接属于Object More global non-object instance methods like gem go in the Kernel module to signify that they are simply methods available everywhere. gem这样的更多全局非对象实例方法在Kernel模块中表示它们只是在任何地方都可用的方法。


Some more about class objects and inheritance... 关于类对象和继承的更多内容......

class Foo < Bar
  def hi
    puts 'Hi!'
  end
end

What this does is really quite awesome. 这样做真的很棒。 It defines a class object, of course. 当然,它定义了一个类对象。 Now this class object is configured to have a name Foo , a parent class Bar and a method hi . 现在,这个类对象被配置为具有名称Foo ,父类Bar和方法hi This info is sort of like this class object's meta data. 此信息有点像此类对象的元数据。

Now the class object Foo itself is an instance of Class . 现在,类对象Foo本身就是Class一个实例 But Foo defines a class that inherits from Bar . 但是Foo 定义了一个继承自Bar的类。 The Class class defines a data structure to store this meta data about a class. Class类定义了一个数据结构来存储关于类的元数据。

You can think of the Class class sorta kinda being defined like this: 你可以认为Class类sorta有点像这样定义:

class Class < Module

  # fictional method called on class creation
  def set_meta_data(name, superclass, methods)
    @name = name
    @superclass = superclass
    @methods = methods
  end

  # fictional way in which an instance might be created
  def new
    instance = Object.new
    instance.superclass = @superclass
    instance.addMethods(@methods)
    instance
  end
end

So a class object itself would inherit from Class but it would create objects that do not. 所以类对象本身将从Class继承,但它会创建不包含的对象。

Thinking of classes as objects can be a bit mind bending in this way, but this also why ruby is awesome. 将类作为对象的思考可能会以这种方式弯曲,但这也是为什么ruby非常棒。

For 1 and 5, pseudo-keyword commands tend to go into Kernel rather than Object. 对于1和5,伪关键字命令倾向于进入内核而不是对象。

For 2, it makes sense for sub-classes to be "down" relative to their parent class (sub literally meaning "beneath"). 对于2,子类相对于它们的父类是“向下”是有意义的(子字面意思是“在下面”)。 Therefore if you're heading for a parent class and its ancestors, you have to go "up". 因此,如果您要前往父母班级及其祖先,您必须“向上”。

For 3, an object object is not an instance of Class , it is an instance of Object . 对于3, object对象不是Class的实例,它是Object的实例。

For 4, what's wrong with something being an instance of Class and inheriting from Object? 对于4,什么是Class的实例并从Object继承有什么问题? All classes inherit from Object. 所有类都继承自Object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM