类对象、单例类
我在 ruby 中玩元编程,我有一个问题。我有一堂课:
class Klass
class << self
@x = "yeah"
end
end
b = Klass.new
a = class << Klass; self; end
a.instance_eval "@x" #=> yeah
Klass.instance_eval "@x" #=> nil
为什么?在变量 a
中我有一个单例类,对吗?并且在单例类的上下文中执行Klass.instance_eval
:
Klass.instance_eval "def yeah; puts 10; end"
Klass.yeah #=> 10
另外,解释器中的Klass
指向类的上下文,是吗?并且 a
指向单例类的上下文? 哪个表示 a.class_eval
和 a.instance_eval
?我明白:
a.instance_eval "def pops; puts 0; end"
a.class_eval "def popsx; puts 1; end"
a.pops #=> 0
a.popsx # FAIL
Klass.pops # FAIL
Klass.popsx #=> 1
b.pops; b.popsx # DOUBLE FAIL
但我不明白这一点。谢谢!
I play with metaprogramming in ruby and I have a question. I have a class:
class Klass
class << self
@x = "yeah"
end
end
b = Klass.new
a = class << Klass; self; end
a.instance_eval "@x" #=> yeah
Klass.instance_eval "@x" #=> nil
Why? In variable a
I have a singleton class, right? And Klass.instance_eval
exec in context of a singleton class:
Klass.instance_eval "def yeah; puts 10; end"
Klass.yeah #=> 10
Also, Klass
in interpreter points to context of class, yes? And a
points to context of a singleton class?
And which indicates a.class_eval
and a.instance_eval
? I do:
a.instance_eval "def pops; puts 0; end"
a.class_eval "def popsx; puts 1; end"
a.pops #=> 0
a.popsx # FAIL
Klass.pops # FAIL
Klass.popsx #=> 1
b.pops; b.popsx # DOUBLE FAIL
and I do not understand this. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,虽然看起来eigentclass被一些人使用,但singleton class是更常见的术语。 Singleton 类包含 Ruby 中对象的特定于对象的行为。除了该单例类所属的原始对象之外,您无法创建该类的其他实例。
谈论不同类型的 eval 中的方法定义这篇文章 为
instance_eval
和class_eval
中定义的方法引入了很好的规则:这几乎描述了情况。
有一篇关于 Class 类的实例的类、作为 Class 类的子类的单例类以及其他一些疯狂的东西(与问题没有多大关系)的文章。但是,由于您的问题可以轻松应用于常规对象及其类(并且这使得事情更容易解释),我决定删除所有内容(不过,您仍然可以在答案的修订历史记录中看到这些内容)。
让我们看看常规类和该类的实例,看看它们是如何工作的:
不同类型的 eval 中的方法定义:
定义特定于对象的方法:
现在让我们看看对象
a
的单例类:现在让我们看看实例变量
:现在到
class_eval
中的实例变量:现在,如果将类
A
替换为类Class
,将对象a
替换为对象 < code>Klass (在这种特殊情况下只不过是类Class
的实例)我希望您能得到对您问题的解释。如果您还有一些,请随时询问。First, while it seems like eigentclass is used by some people singleton class is more common term. Singleton class contains object-specific behavior for an object in Ruby. You can't create other instances of that class except the original object this singleton class belongs to.
Speaking about defining of methods inside different types of eval this article introduces nice rule for methods defined in
instance_eval
andclass_eval
:That pretty much describes the situation.
There was a huge write-up about classes that are instances of Class class, their singleton classes that are subclasses of Class class and some other crazy stuff (not that much related to the problem). But as your question can be easily applied to regular objects and their classes (and it makes things much easier to explain), I decided to remove that all (though, you can still see that stuff in revisions history of the answer).
Let's look at regular class and instance of that class and see how that all works:
Method definitions inside different types of eval:
Defining object-specific methods:
Let's now look at singleton class of object
a
:Now let's look at instance variables:
And now to instance variables inside
class_eval
:Now if you replace class
A
with classClass
and objecta
with objectKlass
(that in this particular situation is nothing more than instance of classClass
) I hope you'll get explanation to your questions. If you still have some feel free to ask.很难完全回答您的问题(有关 Ruby 类模型的彻底解释,请参阅 Dave Thomas 的出色演示),不过:
使用 class_eval,您实际上定义了实例方法 - 就好像您处于 班级。例如:
使用 instance_eval,您实际上定义了类方法 - 就好像您位于给定对象的单例(eigenclass)类的主体内(注意类在 Ruby 中也是对象)。例如:
在您的情况下:
Klass.instance_eval "@x"
不起作用,因为@x
不是 Klass 的一部分,它是 Klass 单例类的一部分:< code>a.instance_eval "@x" 工作正常,因为您在
a
单例类的上下文中计算“@x”,该单例类与您在其中使用的 Klass 类的单例类连接定义了@x
实例变量。单例类如何互连可以通过以下示例来说明:Klass.instance_eval "def yes; put 10; end"
定义一个“普通”类方法。因此,Klass.yeah
工作正常(请参阅前面示例中的Klass.class_method
)。a.instance_eval "def pops; puts 0; end"
定义a
单例类的类方法。因此,a.pops实际上意味着调用pops类方法(同样,这就像调用Klass.class_method)。a.popsx
不起作用,因为您首先必须创建a
的实例才能在其上调用 popsx(但不可能创建 a 的新实例)单例类)。Klass.pops
不起作用,因为 Klass 的单例类中没有定义任何pops
方法(pops
是在a 中定义的)
的单例类)。Klass.popsx
之所以有效,是因为使用a.class_eval "def popsx; put 1; end"
您已经定义了 popsx 实例方法,然后可以在 Klass 对象上调用该方法。在某种程度上,它与这个例子类似:希望它有帮助。
It's hard to completely answer your question (for a thorough explanation of Ruby's class model, look at Dave Thomas' excellent presentation), nevertheless:
With class_eval, you actually define instance methods - it's as if you were inside the body of the class. For example:
With instance_eval, you actually define class methods - it's as if you were inside the body of the singleton (eigenclass) class of the given object (nota bene that classes are objects too in Ruby). For example:
And in your case:
Klass.instance_eval "@x"
does not work because@x
is not part of Klass, it's part of Klass' singleton class:a.instance_eval "@x"
works fine because you evaluate "@x" in the context of thea
singleton class that is connected with the singleton class of Klass class in which you defined the@x
instance variable. How singleton classes can be interconnected can be illustrated by this example:Klass.instance_eval "def yeah; puts 10; end"
defines a 'normal' class method. ThereforeKlass.yeah
works fine (seeKlass.class_method
in the previous example).a.instance_eval "def pops; puts 0; end"
defines a class method on thea
singleton class. Therefore,a.pops
actually means calling thepops
class method (again, it's as if callingKlass.class_method
).a.popsx
does not work because you would first have to create an instance ofa
to be able to call popsx on it (but is not possible to create a new instance of a singleton class).Klass.pops
does not work because there isn't anypops
method defined in Klass' singleton class (pops
is defined ina
's singleton class).Klass.popsx
works because witha.class_eval "def popsx; puts 1; end"
you have defined the popsx instance method which you then call on the Klass object. It is, in a way, similar to this example:Hope it helps.