Hi --

On Sun, 20 Aug 2006, Jan Svitok wrote:

> On 8/19/06, Paul <prcorcoran / comcast.net> wrote:
>> Hi Gary,
>> 
>> I'm not quite sure what you mean by using "an instance variable of the
>> class object". I am familiar with class variables and instance
>> variables. In my case I wanted to cache the data in a class variable
>> because *any* instance could reference the single class variable. Is
>> there yet another type of variable (other than temporary method scope
>> variables) that one could define?
>> 
>> Thanks,
>> Paul
>
> This comes from Ruby's object model (and this may sound familiar to
> you as a smalltalker): in Ruby, everything is an object, so any class
> is an object as well. A class is an instance of class Class. Therefore
> any class can have its instance variables (because it is an object) in
> addition to class variables (class' special behaviour).
>
> Class variables (@@variable) are shared among all sublasses of the class.
>
> Class instance variables are those, that are defined in class methods i.e.
>
> class A
> def self.m()
>    @a = 1 # <- HERE!
> end
> end
>
> or:
>
> class A
> class << self
>   def m()
>       @a = 1 # <- HERE!
>   end
> end
> end
>
> These are not shared among subclasses.

Just one additional comment: you'll also see instance variables of
class objects outside of any method definition, like this:

   class C
     @var = 1
   end

etc.  Every instance variable belongs to 'self' -- so whenever 'self'
is a class, you're seeing instance variables that belong to that
class.


David

-- 
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black     => book, Ruby for Rails
http://www.rubycentral.org       => Ruby Central, Inc.