Accessing a constant

后端 未结 4 1719
一个人的身影
一个人的身影 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:26

    module A; end
    A.class_eval{B=1}
    B # Undefined
    A::B # 1
    

    As for why it works, I'm not really sure. I occasionally use metaprogramming like this when creating very meta frameworks such the Small Eigen Collider, but not in day-to-day work.

提交回复
热议问题