From: Austin Ziegler Date: 2006-04-08T10:12:53+09:00 Subject: Re: Simple Question About Deleting Instances On 4/7/06, Nathan Olberding wrote:> Austin Ziegler wrote:>> So using attr_accessor doesn't declare a variable; it declares a pair>> of methods that access and may instantiate a variable, but doesn't>> have to. And *that* is why doing "attr_accessor :@var" really>> wouldn't be appropriate.> I guess to put my question simply, why is there a single context in> which I can define class variables but not class methods (accessors> for these variables)? Shouldn't the two be handled in the same context> / scope / block / area-of-code? No. To clarify my statement: #attr_accessor creates a pair of instance methods that access and may instantiate an instance variable, but don't have to. Instance variables are always bound to a particular value of self. (I'mnot *quite* sure where class variables reside.) >> class Foo >> p "#{self.inspect} (#{self.object_id})" >> class << self >> p "#{self.inspect} (#{self.object_id})" >> def myself >> p "#{self.inspect} (#{self.object_id})" >> end >> end >> myself >> def myself >> p "#{self.inspect} (#{self.object_id})" >> end >> end "Foo (23584680)" "# (23584668)" "Foo (23584680)" => nil >> Foo.new.myself "# (23581668)" => nil >> So, within the scope of "class Foo", self is a particular object (knownas Foo, oddly enough ;). Within the "class << self" inside of "classFoo", self is a wholy different object. Within the instance methoddeclared in "class << self", self is once again the Foo object. Calling "myself" from the scope of "class Foo" demonstrates this. Icould call Foo.myself can get the same result. What that shows is thatif I use @var inside of the scope of "class Foo", I'm *actually* usingan instance variable inside of that object (of type Class). Where you seem to be having problem is that unlike other languages,a simple declaration in Ruby isn't. Declaration is *execution*. There isno "compile" stage followed by a "run" stage. The two happen together(as demonstrated above). "class Foo", "class << self", and "def myself" all change scope and themeaning of "self" in that scope. Does that help? -austin--Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca