From: "David A. Black" Date: 2003-12-03T23:13:37+09:00 Subject: Re: Attempted roadmap of future instance variables.... Hi -- On Wed, 3 Dec 2003, Robert Klemme wrote: > > "Yukihiro Matsumoto" schrieb im Newsbeitrag > news:1070389195.767662.15370.nullmailer@picachu.netlab.jp... > > Hi, > > > > In message "Re: Attempted roadmap of future instance variables...." > > on 03/12/03, Steve Tuckner writes: > > > > |OK so the jist of it is that @_var variables are stored with the class > of > > |the instance but the class has no access to that. That is all. Right? > > > > No. @_var variables are stored in the instance, but they are > > accessible only from methods defined in the same class. > > Still a number of questions arise to me: How then are @_ variables treated > with respect to methods instance_variables, instance_variable_set and > instance_variable_get? Are they ignored and there is a second set of > methods that receives the class as argument (e.g. > instance_local_variables, instance_local_variable_get, > instance_local_variable_set) I assume they work as always, constrained only by the scope. So: class A def m instance_variable_set("@x",1) instance_variable_set("@_y",2) end def n # @x and @_y, if used here, are the same objects here as above end end class B def mm # @x same as above # @_y different (class [B] local) end end > If so, what, if they are defined in an instance's private class like this: > > class < def get_foo; @_foo; end > def set_foo(f); @_foo = f; end > end > > Do we use the instance instead of the class as method argument to those > methods (see above) to get access to them via those methods? If so, how > is that disambiguited if obj is a class? Applying the rule: @_ variables are visible only from methods defined in the same class, I guess that would mean if you did: class A def x; @x; end def x=(y); @x=y; end def _x; @_x; end def _x=(y); @_x=y; end end a = A.new class << a def showx; @x; end # same @x as in A#x and A#x= def show_x; @_x; end # different @_x (same object (a) but defined in a method in a different class (a's singleton as opposed to A) end I think (?) this is similar to Guy's example with singleton methods (which I'm still puzzling over :-) > Or do we make them unique by modifying the identifier, e.g. @_var becomes > @Foo:_var if it was defined in class Foo? I don't think so; that kind of direct access to instance vars would break privacy and encapsulation. David -- David A. Black dblack@wobblini.net