Accessing a constant

后端 未结 4 1694
一个人的身影
一个人的身影 2021-01-18 12:33

Why can\'t I access \'B\' in the following from \'A\' but can from the main environment?

module A; end
A.instance_eval{B=1}

B #=> 1
A::B #=> uninitial         


        
4条回答
  •  无人及你
    2021-01-18 13:12

    Because . . .

    . . . constants that are not defined within a class or module are given global scope.

    What matters for constant definition is the enclosing lexical scope, not the current receiver or the value of self.

提交回复
热议问题