From: dblack@... Date: 2003-01-11T03:18:19+09:00 Subject: Re: attr_reader/writer vs. attr_accessor w/ access control Hi -- On Sat, 11 Jan 2003, Mauricio [iso-8859-1] Fern�ndez wrote: > On Fri, Jan 10, 2003 at 08:38:50AM +0900, Tom Sawyer wrote: > > On Thursday 09 January 2003 03:57 pm, Mauricio Fern�ndez wrote: > > > That would not happen if accessors were, say, protected by default. But > > > then you'd have to 'publicize' them anyway, and the situation wouldn't > > > be that different from the current one. > > > > > > OTOH if instance variables were private (not accessible by subclasses) > > > all this would be much more relevant. I really felt that this was > > > The Right Thing but matz doesn't [55201]. I think the main point was > > > that inheritance is anyway a very strong relationship, so this apparent > > > breach in data hiding is not that important. > > > > that's it! that's it! i've been pulling my hair out since the beginning of my > > rubyist days deciding when it is the Right Thing to use @var vs. self.var. > > And finally it has become clear! the answer is simple: NEVER USE @ OUTSIDE A > > GETTER OR SETTER METHOD. what, you say? indeed! in fact, with use of the > > Then take into account that this goes against matz's word [55201], for > if @var were never to be used, it would not exist in the language and > instance vars. would be class-local by default. > I have to ponder about this some more, but I have the feeling @iv is > needed sometimes. ruby-1.6.8$ grep "^[[:space:]]\+@[A-Za-z_]\+ \?= \?" \ `find . -name '*.rb'` | wc -l 1026 :-) In fact, I tend to think the attr_* shortcuts, while very useful, sometimes lead to the impression that there's a closer connection between attribute accessor methods and instance variables than there really is. The presence of such methods, and the use of instance variables, can vary independently. For example: class A def some_attribute 100 end end or class B def some_attribute @var end end or class C attr_reader :some_attribute def some_attribute=(x) @some_attribute = x * 10 end end and so on. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav