class_eval 和 class << 有什么区别类名?
我是红宝石初学者。我发现这两者非常相似(在输出中),但我无法理解以下上下文中的差异。例如,我有一个类
class Say
def self.hello
puts "hello"
end
end
,可以像这样扩展
class << Say
def hi
puts "hi"
end
end
,也可以像这样扩展。
Say.class_eval do
def self.bye
puts "bye"
end
end
我什么时候应该使用 <<
以及什么时候使用 class_eval
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
class_eval
与class << 没有任何关系。类名
。相当于
有一些差异。 class_eval 使用一个块(或一个字符串,但暂时忽略它),这意味着它关闭了包含的词法范围。换句话说,您可以使用周围范围内的局部变量。公共类块引入了一个全新的范围。同样,您可以创建块并将其传递给许多不同的 class_eval,块的主体将在您调用 class_eval 的类的上下文中执行。
类<< className
打开className
的单例类,允许您定义类方法。与注意相同
,如果 A 恰好是一个类(几乎)Ruby 中的所有对象都有单例类,并且您可以使用这两种语法之一为它们定义方法,则它们是唯一的类方法。
class << 的优点obj
主要是如果您一次性定义许多单例方法。class_eval
doesn't really have anything to do withclass << className
.is equivalent to
with a few differences. class_eval uses a block (or a string, but ignoring that for the moment) which means it closes over the containing lexical scope. In other words you can use local variables from the surrounding scope. The common class block introduces a brand new scope. Likewise you can create the block and pass it to many different class_eval's, and the body of the block will be executed in the context of the class you are calling class_eval on.
class << className
opens the singleton class ofclassName
, allowing you to define class methods.Is the same as
Note that they are oly class methods if A happens to be a class (almost) all objects in ruby have singleton classes and you can define methods for them using either of those two syntaxes. The advantage of
class << obj
is mainly if you're defining many singleton methods in one go.正如已经说过的那样,即使它们在您的示例中似乎做了相同的事情,但 class_eval 实际上并没有多大关系
(虽然效果相似,但它的作用并不相同,存在细微的差异)。
这是另一个示例,其中第二种形式的用法更加清晰:
a 和 b 都是同一类 A 的对象,但我们向 b 的元类添加了一个方法,因此方法 say_hi 仅适用于 b 对象。
As already said class_eval has really not much to do with
even if they seem to do the same thing in your example (while the effect is similar it does not do the same, there are subtle differences).
Here is another example where the usage of the second form is far more clearer:
a and b are both objects of the same class A but we added a method to the metaclass of b so the method say_hi is only available to the b object.