Is there a short way to do this?
def value
@val
end
def value=(value)
@val = value
end
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.
No, there is not. All attr_accessor does is define two methods, one called value (in this case) and the other called value= that set an instance variable of the same name. Since you should only be accessing the instance variable via the getter/setter methods, it shouldn’t matter what it is called internally.
If you are inheriting, you can use a call to super to ensure constancy:
EDIT
If you really want to do it then you can define your own method on Class:
I haven’t tested it, but something like that should work. I’m not saying it’s a good idea, but that is what you’re looking for.
EDIT2
Here is how it works in practice: