From: Christoph (news_chr) Date: 2003-12-10T17:37:07+09:00 Subject: Re: Attempted roadmap of future instance variables.... T. Onoma wrote: .... > A word of update on this idea. I have been writing my lastest code as if this > were already fact, in order to get a realistic feel for what it would be > like. It works fine except for one signifficant age old problem: the > ambiguity between methods and local variables. Because instance variables > would be local to their class, one would then uses writer methods from mixins > and subclasses and so forth. Unfortunately you always have to use self as a > reciever on these to prevent them from being local variables. The age old > ambigutiy rears its head again. Quick ex- > > class K > attr_writer :h > end > class Q < K > def hi(x) > self.h = "Hello, #{x}" # must use self > end > end > > Honestly, I would prefer using % for local variables and kill this ambiguity > once and for all! This is main the reason why I think that instance variables shouldn't be local to singleton classes. To access instance from the non-singleton super class you would need to write an attribute accessor valid for all instances. In general you would chose the accessibility to be private - as in class A def intialize @foo = "foo" end private attr_accessor :foo end a = A.new def a.bar # the syntactic hind that "foo" is not a local variable # conflicts with the privacy of A#foo= self.foo = "bar" # raises NoMethodError - private end The only way around this to not use a standard attribute writer class A def intialize @foo = "foo" end private attr_reader :foo # need to use a javaish setter:-( def set_foo(rhs) @foo = rhs end end a = A.new def a.bar set_foo("bar") end # or use to a protected or public accessibility for A#foo=. Another possibility - you can consider this to be RCR for Rite (independently of the "class local variable stuff") - is that "private" methods should be callable if method receiver is an explicit self (currently only implicit self's are allowed). class A def bar self.foo # this SHOULD NOT RAISE a NoMethodError end private def foo; end end /Christoph