From: "Ara.T.Howard" Date: 2003-12-06T02:12:05+09:00 Subject: Re: Attempted roadmap of future instance variables.... On Sat, 6 Dec 2003, ts wrote: > Date: Sat, 6 Dec 2003 01:42:43 +0900 > From: ts > Newsgroups: comp.lang.ruby > Subject: Re: Attempted roadmap of future instance variables.... > > >>>>> "A" == Austin Ziegler writes: > > A> What is the problem that @_ variables are trying to solve? > > Well, if I remember correctly the original proposition one of the problem > can be summarized with this stupid example. > > Imagine that someone define a module which implement a counter, he'll > write something like > > module A > def increment > @counter ||= 0 > @counter += 1 > end > > def get > @counter > end > end > > because the instance variable @counter is used only to remember some > internal state, this variable is never documented and in the documentation > there is only a reference to #increment and #get > > Now someone else use this nice module from RAA and write > > class B > include A > > def initialize > @counter = "counter" # he don't know that this variable is used > end > end > > He has completely broken the module A. > > You don't have this problem with class local variable because > * when you call a method of A : @_counter is a fixnum > * when you call a method of B : @_counter is a string so the problem is that there is no concept of 'private' instance variables in ruby. i finally understand - thanks guy. this is a problem, but i also do not like the @_ syntax. perhaps it would be preferrable to be able to do this module A private @counter protected @another_var def increment @counter ||= 0 @counter += 1 end def get @counter end end the default would obviously be public. this would not break existing code. alternatively, if people prefer symbols we could have @var # public instance var _@var # protected instance var __@var # private instance var this also would not break exiting (@_var) code. maybe it would be too hard to parse '_@' and '__@' as atomic token though? -a -- ATTN: please update your address books with address below! =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | STP :: http://www.ngdc.noaa.gov/stp/ | NGDC :: http://www.ngdc.noaa.gov/ | NESDIS :: http://www.nesdis.noaa.gov/ | NOAA :: http://www.noaa.gov/ | US DOC :: http://www.commerce.gov/ | | The difference between art and science is that science is what we | understand well enough to explain to a computer. | Art is everything else. | -- Donald Knuth, "Discover" | | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done' ===============================================================================