From: Dave Thomas Date: 2003-08-17T03:48:45+09:00 Subject: Re: Newbie Q: Data encapsulation with Ruby On Saturday, August 16, 2003, at 12:31 PM, Meino Christian Cramer wrote: > Until this point I thought, "attributes" _are_ instance variables... > (haveing C++ in mind...) > Attributes are methods which give you access to instance variables. As others have said, they are the quivalent of Java's getters and setters. attr_reader :fred is the same as writing def fred @fred end Ruby is good this way: objects can choose to expose their state directly, but then they can change to having more complex method-based access. The key thing is that this change is transparent to users of the object: when you say f.age you have no idea if you're getting the age as a transparent reference to an instance variable or as the result of some computation. Read Meyer's Object Oriented Software Construction for a lot of information on this - he calls it the Uniform Access Principle, and it's one way of increasing encapsulation and decreasing coupling. Cheers Dave