- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
当我将 lambda 作为 block 传递给 instance_eval
时,它似乎传递了一个额外的参数:
lamb = -> { puts 'hi' }
proc = Proc.new { puts 'hi' }
instance_eval(&lamb)
# >> ArgumentError: wrong number of arguments (given 1, expected 0)
# from (irb):5:in `block in irb_binding'
# from (irb):7:in `instance_eval'
instance_eval(&proc)
# => hi
instance_exec(&lamb)
# => hi
为什么会这样?请注意,此问题与 lambda 为何抛出错误无关。这是很好理解的。问题是关于为什么 instance_eval
将接收方的 self
作为参数发送。它不是必需的,而且令人困惑。并且 AFAIK 没有记录。
This有帮助,但没有解释为什么 ruby 会这样做。 instance_eval
的重点是将 self
设置为接收者;为什么还通过将 self
传递给 proc 来混淆事情?
最佳答案
来自文档
For procs created using lambda or ->() an error is generated if the wrong number of parameters are passed to a Proc with multiple parameters. For procs created using Proc.new or Kernel.proc, extra parameters are silently discarded.
在你的例子中,lamb
和 proc
都使用一个参数调用
From the docs of instance_eval
When instance_eval is given a block, obj is also passed in as the block's only argument
instance_eval
是BasicObject
类的方法,可以在实例中调用。例如,给定的 block 将可以访问私有(private)方法。
class Test
def call
secret_number + 100
end
private
def secret_number
42
end
end
test = Test.new
show_secret = -> (obj) { puts secret_number }
test.instance_eval(&show_secret) # print 42
没有当前上下文的实例 self
将作为参数传递。我认为 instance_eval
更适合在对象中调用它。
From the docs of instance_eval
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 and private methods.
关于Ruby lambda 的 proc 和 'instance_eval',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52618934/
我有以下代码: class A def self.scope yield end def self.method_added method self.instance_ev
我对 instance_eval 的理解是,如果我有模块 M,那么以下是等价的: module M def foo :foo end end class C class true
单例方法是一种只在一个实例上定义的方法。 foo = Foo.new def foo.case #singleton method end instance_eval 不是做同样的事情吗?为特定实
我正在尝试一些 ruby 元编程,但对 instance_eval() 感到有些困惑。 看下面的例子 @instance_var = 'instance_var' local_var = 'loc
虽然我已经使用了一段时间,但我似乎对这两种方法感到困惑,我无法理解为什么方法 passengers 没有被添加到以下代码中的对象中: class Bus def number_of_seats
看着这个instance_eval示例: class KlassWithSecret def initialize @secret = 99 end def g
我正在尝试调用对象 foo 的方法,就好像它是对象 bar 的方法一样。我尝试了两种方法: 1。解除绑定(bind)和绑定(bind) - 由于类不同而失败 class Foo def initi
当我在 instance_eval block 中为类定义一个方法时,它会创建一个很好的类方法。 例如) class A end A.instance_eval do def method; en
我想对 DSL 进行一些改进。我能够使用此示例进行改进: module ArrayExtras refine Array do def speak puts 'array!'
Foo = Class.new Foo.instance_eval do def instance_bar "instance_bar" end end puts Foo.instan
class_eval 有什么区别吗? & instance_eval工作除了def ?里面class_eval block def定义类自身的方法(即实例方法)和内部 instance_eval de
我发现传递给 class_eval、module_eval 和 instance_eval 的行号与错误报告的行号不匹配。 ruby-doc 未解释此行为其中说:(以 instance_eval 为例
伙计们。我创建了一个类: class A def initialize &b instance_eval &b end def method_missing method_id,
完整代码:http://friendpaste.com/5TdtGPZaEK0DbDBa2DCUyB class Options def method_missing(method, *arg
我正在研究 Pickaxe 1.9,我对 instance/class_eval block 中的常量查找感到有点困惑。我正在使用 1.9.2。 似乎 Ruby 在 *_eval block 中处理常
我了解 instance_eval 和 class_eval 之间的基本区别。我在玩弄时发现的是一些涉及 attr_accessor 的奇怪东西。这是一个例子: A = Class.new A.cla
class Foo include Module.new { class_eval "def lab; puts 'm' end" } def lab super
我知道 send 接受带有参数的字符串或符号,而 instance_eval 接受字符串或 block ,并且它们的区别在给定接收者时可能很明显。 我的问题是下面示例的“幕后”区别是什么? 1234.
如果我这样做 def eval_file(file) instance_eval read(file) end 然后,一旦文件内的方法/ block 之一发生某些事情,我所看到的就是“eval_f
我最近尝试做类似的事情: a = "some string" b = Proc.new{ upcase } a.instance_eval b 这给出了错误: TypeError: can't con
我是一名优秀的程序员,十分优秀!