- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
Foo = Class.new
Foo.class_eval do
def class_bar
"class_bar"
end
end
Foo.instance_eval do
def instance_bar
"instance_bar"
end
end
Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Class
Foo.new.class_bar #=> "class_bar"
Foo.instance_bar #=> "instance_bar"
Foo.new.instance_bar #=> undefined method ‘instance_bar’ for #<Foo:0x7dce8>
仅根据方法的名称,我希望 class_eval 允许您向 Foo 添加类方法,而 instance_eval 允许您向 Foo 添加实例方法。但他们似乎在做相反的事情。
在上面的例子中,如果你在 Foo 类上调用 class_bar,你会得到一个未定义的方法错误,如果你在 Foo.new 返回的实例上调用 instance_bar,你也会得到一个未定义的方法错误。这两个错误似乎与对 class_eval 和 instance_eval 应该做什么的直观理解相矛盾。
这些方法之间的真正区别是什么?
class_eval 的文档:
mod.class_eval(string [, filename [, lineno]]) => obj
Evaluates the string or block in the context of mod. This can be used to add methods to a class.
instance_eval 的文档:
obj.instance_eval {| | block } => obj
Evaluates a string containing Ruby source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables.
最佳答案
如文档所述,class_eval
在模块或类的上下文中评估字符串或 block 。所以下面的代码是等价的:
class String
def lowercase
self.downcase
end
end
String.class_eval do
def lowercase
self.downcase
end
end
在每种情况下,都重新打开了 String 类并定义了一个新方法。该方法适用于该类的所有实例,因此:
"This Is Confusing".lowercase
=> "this is confusing"
"The Smiths on Charlie's Bus".lowercase
=> "the smiths on charlie's bus"
class_eval
与简单地重新打开类相比有很多优势。首先,您可以轻松地在变量上调用它,并且很清楚您的意图是什么。另一个优点是,如果该类不存在,它将失败。所以下面的示例将失败,因为 Array
拼写错误。如果类只是重新打开,它将成功(并且将定义一个新的不正确的 Aray
类):
Aray.class_eval do
include MyAmazingArrayExtensions
end
最后 class_eval
可以接受一个字符串,如果你正在做一些更邪恶的事情,这会很有用......
instance_eval
另一方面,针对单个对象实例评估代码:
confusing = "This Is Confusing"
confusing.instance_eval do
def lowercase
self.downcase
end
end
confusing.lowercase
=> "this is confusing"
"The Smiths on Charlie's Bus".lowercase
NoMethodError: undefined method ‘lowercase’ for "The Smiths on Charlie's Bus":String
因此对于 instance_eval
,该方法仅为字符串的单个实例定义。
那么为什么 Class
上的 instance_eval
定义类方法呢?
正如"This Is Confusing"
和"The Smiths on Charlie's Bus"
都是String
实例,Array
、String
、Hash
和所有其他类本身都是 Class
的实例。您可以通过对它们调用 #class
来检查这一点:
"This Is Confusing".class
=> String
String.class
=> Class
因此,当我们调用 instance_eval
时,它对类的作用与对任何其他对象的作用相同。如果我们使用 instance_eval
在类上定义一个方法,它将只为该类实例定义一个方法,而不是为所有类定义一个方法。我们可能将该方法称为类方法,但它只是该特定类的实例方法。
关于ruby - 如何理解class_eval()和instance_eval()的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/900419/
我这样做有什么区别 class T def initialize self.class.class_eval do def test return self.cl
在以下情况下如何传递参数名称..在传递给 class_eval 之前评估名称 class Foo end Foo.class_eval %Q{ def hello(name) p "hello #
我正在使用 Spree,Spree 有一个名为 Order 的类,如下所示: module Spree class Order # class definition. end end
我正在尝试弄清楚如何动态创建方法 class MyClass def initialize(dynamic_methods) @arr = Array.new(dynamic_method
我正在使用 class_eval 编写要在当前类的上下文中执行的代码。在下面的代码中,我想为属性值的变化添加一个计数器。 class Class def attr_count(attr_name)
我不明白 class_eval。 class Module def attr_ (*syms) syms.each do |sym| class_eval %{def #{sy
是否可以临时将某些方法应用到类中进行测试?我希望能够根据多种应用方式运行规范。虽然我可以制作一堆具有不同设置的装置,但我发现在测试中仅 class_eval 模型更容易。例如: describe "s
我正在尝试运行以下命令,但我不断收到 "NoMethodError: undefined method `bar=' for #/Foo:0x86f69ba/" class Class
虽然我已经使用了一段时间,但我似乎对这两种方法感到困惑,我无法理解为什么方法 passengers 没有被添加到以下代码中的对象中: class Bus def number_of_seats
我试图扩展 code from this question用于保存属性值的记录。但是,如果有多个属性,我的代码就会失败。这是代码: class Class def attr_accessor_wi
我正在测试元编程,但有一个案例我不明白: module Bar def self.append_features klass klass.class_eval do def self
我想写一个方法,它接受一个参数并创建另一个方法,用这个参数命名。这是我的代码 class Class def createMethod(attr_name) attr_name = att
我对使用 class_eval 的 Ruby 变量范围的最佳实践有疑问。 在这段代码中,在 class_eval block 中,局部变量 first 和 second 是未定义的。 def pare
我知道可以使用 class_eval 定义实例方法。是否可以在 class_eval 的上下文中定义类方法? 最佳答案 是的,这是可能的: class Foo end Foo.class_eval d
class_eval 有什么区别吗? & instance_eval工作除了def ?里面class_eval block def定义类自身的方法(即实例方法)和内部 instance_eval de
class String def hello "world" end end String.class_eval { def world "hello" end } "
我正在学习 SaaS 斯坦福类(class),尝试完成 this assignment 的第 5 部分 我很难理解这个概念,这就是我试图做的: class Class def attr_acces
为什么我无法访问 class_eval block 中的变量 vehicle: class Vehicle def self.number_of_wheels fail 'DEFINE i
我发现传递给 class_eval、module_eval 和 instance_eval 的行号与错误报告的行号不匹配。 ruby-doc 未解释此行为其中说:(以 instance_eval 为例
test = "a" test.class_eval do # what is going on here??? end 最佳答案 ActiveSupport 将 class_eval 添加到 O
我是一名优秀的程序员,十分优秀!