Can anybody say me, why that isn’t working:
class A
attr_accessor :b
end
a = A.new
a.instance_eval do
b = 2
end
a.b
=> nil
What is wrong i’m doing?
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 culprit lies in this part of the code:
Although
b = 2is evaluated in the context of your instance, it doesn’t call the setter. Instead it just creates a new local variable calledbin the current scope. To call the setter, you have to further clarify your code to resolve the ambiguity: