From: Ross Bamford Date: 2005-11-27T01:02:28+09:00 Subject: Re: Nubish questions about syntax and gems On Sat, 26 Nov 2005 15:24:36 -0000, David A. Black wrote: > There's a simple way to get a handle on instance variables: > > At every moment during runtime, there is a current or default object > -- "self". Every time you see: > > @var > > you are seeing an instance variable that belongs to "self", the > current object. There's never any ambiguity. > > "self", in turn, can be any object, including a Class object. So, > when in doubt, ask what "self" is: > > class C > puts "In class definition body scope" > puts self # C (the class object) > @var = 1 > puts @var # 1 > > def some_method > puts "In instance method scope" > puts self # an instance of C > puts @var # nil > end > end > > c = C.new > c.some_method > > As you'll see from the output, the fact that the object C (the Class > object) assigns to its instance variable @var does not affect the @var > of an *instance* of C (which is uninitialized and therefore nil). > > You can think of class objects as wearing two hats: the "special case" > hat of being an object factory, and the "civilian" hat of just being > an object. It's in "civilian" mode that class objects possess > instance variables of their own, on the same footing as all other > objects. > David, That's a good explanation, thanks for that :) So I guess it makes sense that I can't access, say @var, in the class as @@var in instance methods. I think I was adding extra confusion for myself with that assumption. Theres a lot of scope in these nuances, my mind is starting to work overtime ;) Thanks again! -- Ross Bamford - rosco@roscopeco.remove.co.uk