On Thursday 09 January 2003 03:57 pm, Mauricio FernŠŌdez 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 
shortcut attr_* methods, i never even have to see another instance variable 
agian (at least in my code). in almost all cases this means all my attr_* 
calls will be attr_accessor, but i can utilize access control to make them 
visible or not to the outside rather than by not defining them. i have 
already created a fairly goods means to do this concisely, though i would 
still like to refine it a bit more:

	public
	privatize writers attr_accessor :x, :y
	protect readers attr_accessor :a, :b

	private
	publicize attr_accessor :n, :w => :to_s

	etc.

examine the first example. i modified #attr_accessor to additionally return 
the method symbols it creates:

	attr_accessor :x, :y   #--> [:x, :x=, :y, :y=]

then #writers filters those to return only the ones with equals on the end:

	writers [:x, :x=, :y, :y=]   #--> [:x=, :y=]

and of course privatize make these two setter methods private.

ah, i like this. only a couple questions left. 1) what's the best way to 
encode what i've done above (i think what i have may be too long winded) and 
2) given this approach, under what circumstances would one ever need 
attr_reader and attr_writer?

thanks millions!

-- 
tom sawyer, aka transami
transami / transami.net


                                   .''.
       .''.      .        *''*    :_\/_:     .
      :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.
  .''.: /\ :   ./)\   ':'* /\ * :  '..'.  -=:o:=-
 :_\/_:'.:::.  | ' *''*    * '.\'/.' _\(/_'.':'.'
 : /\ : :::::  =  *_\/_*     -= o =-  /)\    '  *
  '..'  ':::' === * /\ *     .'/.\'.   '._____
      *        |   *..*         :       |.   |' .---"|
        *      |     _           .--'|  ||   | _|    |
        *      |  .-'|       __  |   |  |    ||      |
     .-----.   |  |' |  ||  |  | |   |  |    ||      |
 ___'       ' /"\ |  '-."".    '-'   '-.'    '`      |_.
------------------------------------------------------------