Learn Ruby on Rails: the Ultimate Beginner's Guide
Notice: This is a discussion thread for comments about the SitePoint article, Learn Ruby on Rails: the Ultimate Beginner's Guide.
I hope you haven't printed too many of these ;-).
& I'm sorry if this is not correct; I'm too lazy to test it ;-).
The example code contains an error.
The second line is misleading. It *is* an instance variable, but not an instance instance variable, it's a class instance variable: the instance variable of the Car object, not a Car instance. So there are *two* distinct variables in the example: the class' instance variable and the instances instance variable (which will be created in the setter method). This is a pretty fundamental thing: I think you do not understand Ruby's object model :(. Ruby is not Java: classes are objects and you initialize instance variables in instance methods (initialize() for example) and not in the class. I hope you explain attribute_accessor soon; that may have prevented this kind of error in the rest of the book.
@mileage = 0 # instance variable