3

为什么Binding#eval调用块,但Kernel#eval不调用?

def bar; binding; end
bi = bar {|x| p(x+1)}

bi.eval "yield 2"       # ==> 3
eval "yield 2", binding # ==> LocalJumpError
4

1 回答 1

6

我认为,在第五行,第二个参数必须是“bi”:

$ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]

1 def bar; binding; end
2 bi = bar {|x| p(x+1)}
3
4 bi.eval "yield 2"  #==> 3
5 eval "yield 2", bi #==> 3
于 2013-03-26T21:00:34.960 回答