图中是2.5.1的Ruby的IRB 照道理说,class << Math的内部应该是Math模块的单例类, 在这个单例类中定义的实例方法内部,主调默认应该是Math这个模块 因此应该可以访问Math命名空间内的东西 但为啥Ruby在找PI这个常 ...
最后我想指出,你在一个class的singleton里是无法访问在class内定义的常量。 class A module B; end end class << A B end # NameError: uninitialized constant Class::B 这是因为singleton class的祖先链中并不包含自身的class,直接从Class开始。
查看全部评分
折叠内容标题(非必须)
折叠内容
站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作
GMT+8, 2025-2-22 05:10
Powered by Discuz! X3.1
© 2001-2013 Comsenz Inc.