为什么Binding#eval
会调用该块,但Kernel#eval
不会?
def bar; binding; end
bi = bar {|x| p(x+1)}
bi.eval "yield 2" # ==> 3
eval "yield 2", binding # ==> LocalJumpError
答案 0 :(得分:5)
我认为,在第五行,第二个参数必须是'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