我想要访问在顶层主要对象中定义的变量和方法在程序EVAL和红宝石绑定
@x = :hello
def instanceMethodMain
p "instanceMethodMain"
end
class Baz
def method
p eval("@x", TOPLEVEL_BINDING)
p eval("instanceMethodMain", TOPLEVEL_BINDING)
end
end
Baz.new.method
输出是
:hello
"instanceMethodMain"
"instanceMethodMain"
的输出是相同的,即使我使用
mainRef=TOPLEVEL_BINDING.eval('self')
p mainRef.send :instanceMethodMain
有人可以解释为什么instanceMethodMain被调用两次。