class Polygon
attr_accessor :sides
@sides = 10
end
When I try to access
puts Polygon.new.sides # => nil
I get nil. How to access sides?
What is wrong here?
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.
Since ruby class definitions are just executable code, when you say
@sides = 10in the context of a class definition, you’re defining that onPolygon(not instances of Polygon):You probably want to set the number of sides on the instances of Polygon, from the initializer: