From: Mark Wilson Date: 2003-08-17T04:22:31+09:00 Subject: Re: Newbie Q: Data encapsulation with Ruby On Saturday, August 16, 2003, at 01:31 PM, Meino Christian Cramer wrote: > [snip] > The whole attr_* family of methods really just saves some typing. > > ...but for me it would break down all "walls" and access controls a > class would (should?) have over its instance variables... > > Doesn't such a design tend to become more "procedural" (in opposite > to OOP) than wanted? > [snip] Hello Meino, I think you're raising a question about what OOP really is, particularly when source code is open. There are some objects that hold information that is used by other objects. You want these objects to provide that information when another object asks for it. This is the method attr_reader provides. Sometimes you want an object to permit other objects to update information being held by that object. This is the method attr_writer provides. You can use one or the other or both or neither. Some objects have no methods permitting access to the information they hold -- this kind of object does something in response to a method call by another object, but does not give or receive information that it contains. As the programmer, you see the object from the inside and the outside. If you use test-driven development, you start by looking at the object from the outside and give it methods that will respond as desired when called -- you are focusing on the interface to the object. Once you write the test you then change your (conceptual) position and go inside the object and figure out how you can respond to the method. This often involves using some procedural code (although you can create objects of any available class inside the method as well). Note that the above is all theory. There is no substitute for reading other people's code (check the files in your /lib/ruby/1.8/ directory) and writing your own code. One last note, in Ruby, you don't have to write your own classes and methods to write a functioning program -- you can use objects defined by classes and methods built in or added to Ruby by others and write programs entirely in the "main" object space. Regards, Mark