From: dblack@... Date: 2003-01-10T07:14:24+09:00 Subject: Re: attr_reader/writer vs. attr_accessor w/ access control Hi -- On Fri, 10 Jan 2003, Tom Sawyer wrote: > it strikes me as curious.... > > i've spent quite a bit of time wrestling with some special attr_* module > methods for use with CGI and i was quite concerned with honoring the full > bredth of reader, writer, and accessor even though it meant more work; that > is to say if i just used accessor it would be a lot easier. and that's when > it hit me: if i don't want the external world reading and/or writing to my > object's instance variables, why wouldn't i just set the access control to > private (or protected)? > > class AttrDemo > attr_accessor :readme > protected :readme= > end > > granted this adds in a touch more code, but that's not the point i'm making. > i'm only pointing out that in effect this serves the same purpose as: > > class AttrDemo > attr_reader :readme > end > > the only differenece being that in the first example i have a setter method > available to the class itself. fine with me. > > except for the fact that instance variables can spring into existence > dynamically, i wonder why they aren't simply attr_accessor by default and > access control the sole means by which we make them visible or invisible to > the outside. This would mean, for one thing, that every object would respond to every message; there would no longer be a distinction between those messages an object understood and those it didn't. If you said: s = "abc" s.keys then you'd get the value of the @keys instance variable of s, instead of an indication that s doesn't respond to #keys. And so forth.... Rather more i.v.'s in the landscape than one wants :-) > so, in realizing this, i modified my attr_* methods to return their effected > methods so that i could pass them on to an access control method. > unfortuantely the access control methods will not take arrays. :-( it would > be nice if they used #flatten so they could. You can use the unary * to un-array your arguments (hey, I never noticed the similarity of 'unary' and 'unarray' :-) class A def a; end def b; end c = [:a, :b] private(*c) end A.new.b # error: private method 'b' called David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav