红宝石中的TOPLEVEL_BINDING是什么? [英] What is TOPLEVEL_BINDING in ruby?

查看:103
本文介绍了红宝石中的TOPLEVEL_BINDING是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它不等于主线程上的binding,这个顶级作用域是什么?此作用域与主线程上的binding有什么区别?

It doesn't equal the binding at the main thread, what is this toplevel scope? What does this scope differ from the binding at the main thread?

> ruby -e 'puts TOPLEVEL_BINDING === binding'
false

推荐答案

事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建一个新的Binding实例,该实例封装每次都具有当前执行上下文.在顶层,它们都包含相同的绑定,但是它们不是同一对象,因此您无法使用=====测试它们的绑定相等性.

The fact is, TOPLEVEL_BINDING always refers to a predefined global instance of Binding, while Kernel#binding creates a new instance of Binding that encapsulates the current execution context every time. At top level, they both contain the same bindings, but they are not the same object and you cannot test their binding equality with == or ===.

puts TOPLEVEL_BINDING
puts TOPLEVEL_BINDING
puts binding
puts binding
puts binding == binding

# =>
#<Binding:0x9769ea0>
#<Binding:0x9769ea0>
#<Binding:0x9941ea8>
#<Binding:0x9941e58>
false

这篇关于红宝石中的TOPLEVEL_BINDING是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆