The idiomatic way to do this would be
A.const_set(:B, 1) A::B
As for why this would not work, in Ruby 1.8 and 1.9.2+ (in 1.9.1) it was different; constant search is lexically limited. I found a good blog post with an explanation. Quote:
Please note that these rules apply to the constant definition as well as to the search. In 1.8 and 1.9.2, the constant defined in the class_evaluated block will be defined in the attached lexical field, and not in the receiver volume.
The same is true for instance_eval
.
source share