How to access attr_accessor‘s instance variable from a subclass?
class A
attr_accessor :somevar
@somevar = 123
puts @somevar
end
class B < A
def meth
puts @somevar
end
end
B.new.meth
puts nil…
P.S. I can use ActiveSupport.
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.
You need to set the instance variable in an initialize method — this gets called whenever a new class instance is created: