Can anyone explain the difference between accessing an instance attribute via self.attribute and by @attribute?
Can anyone explain the difference between accessing an instance attribute via self.attribute and by
Share
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.
self.attributecalls the methodattribute.self.attribute = valuecalls the methodattribute=with the argumentvalue.@attributeand@attribute = valueget/set the value of the instance variable@attribute.So basically they’re two entirely different things.
However if you call
attr_accessor :attributeit defines the methodattributeto return@attributeand the methodattribute=(value)to set@attribute = value. So in that case, there is no difference.