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 #=> uninitialized
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The idiomatic way to do this would be
As to why it doesn’t work, in Ruby 1.8 and 1.9.2+ (it was different in 1.9.1), constant lookup is lexically scoped. I found a good blog post with an explanation. To quote:
The same is also true for
instance_eval.