Ruby 获取 BasicObject 实例的 binding

Niatruc · December 16, 2018 · 742 hits

想得到一个 BasicObject 的实例的 binding,但是 BasicObject 的实例方法中没有 binding,于是下面的代码行不通:

bo = BasicObject.new

bo.instance_exec { binding } #=> NameError: undefined local variable or method `binding' for #<BasicObject:0x2e570ded>

https://bugs.ruby-lang.org/issues/5360 这个帖子里得到了解决方法:

bo.instance_exec { ::Kernel.binding }
No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.