From: dblack@... Date: 2003-01-10T09:27:01+09:00 Subject: Re: attr_reader/writer vs. attr_accessor w/ access control Hi -- On Fri, 10 Jan 2003, Tom Sawyer wrote: > 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 You could do: attr_accessor :a, :b, :x, :y private :x=, :y= protected :a, :b which is shorter and (I find) easier to read. I think there's a bit of a diminishing return, in terms of clarity, in wrapping wrappers in wrappers beyond a certain point. > 2) given this approach, under what circumstances would one ever need > attr_reader and attr_writer? Well, if your approach is to write and use a different set of method-creation shortcuts, then possibly never :-) They're awfully nice and concise though. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav