From: Dan Doel <djd15 / po.cwru.edu> Subject: Re: Newbie Q: Data encapsulation with Ruby Date: Sun, 17 Aug 2003 01:02:45 +0900 Hi Dan, > >The whole attr_* family of methods really just saves some typing. > > > >And of course many combinations and permutations are possible... I've > >just used some simple examples to show some of the basic operations of > >attr_*. > > > > > > I'd add that in Java, you "shouldn't" (by OO principles), make your data > public. You should make > all your data private (or protected) and use accessors to get at that > data, like: That is exactly what I thought.... > class Foo > { > private int bar; > > public int getBar() > { > return bar; > } > > public void setBar(int x) > { > bar = x; > } > } > > All Ruby is doing is automating the otherwise tedious processes you need > to use in Java. (in general)...but with the getter/setter (especially setter) you gain more control over what is going in and out. A setter could control the validity of the value range... (I am not saying, that it is NOT POSSIBLE in ruby to write setter/getter methods! This is a more general remark on OOP...) > It's not using any different principles (in fact, it enforces the > recommended principles > better). > > Just an addendum, not a correction. > > - Dan > > P.S.: It's also the case that people can add methods to your class to > access private data > you don't want them to. That's just a part of ruby. I believe the > #freeze method prevents > that, though. > > > Keep hacking! Meino