Currently class variables are also shared between different classes that
are part of the same inheritance tree. IMHO this is a rarely needed
feature and you're better off using regular instance variables on the
class (and referring to them via self.class.var from an instance).

Advertisements

Florian Gross wrote:
> Lionel Thiry wrote:
>> I don't understand (and I'm quite surprised), what is the difference
>> in terms of OO design between class variables, the @@a in the example
>> above, and class instance variables, the @a in the example?
>
>
> Currently class variables are also shared between different classes that
> are part of the same inheritance tree.
I ignored that. *doing some testing* My! You're right.
> IMHO this is a rarely needed
> feature and you're better off using regular instance variables on the
> class (and referring to them via self.class.var from an instance).

The class variable ("@@") belongs to the class object, while the instance
variable (@) belongs to the instanciated object. A short example. May be you
want to count how many objects are created based on a class. If you want to
avoid global variables ($), which is a very good decision, you can use class
variables. Using instance variables will not work.
>>>>> Example >>>>>
class Mytest
@@n_of_Mytest = 0
def initialize
@@n_of_Mytest += 1
end
def Mytest.n_of_Mytest
@@n_of_Mytest
end
end

Wolfgang Nádasi-Donner wrote:
> The class variable ("@@") belongs to the class object, while the instance
> variable (@) belongs to the instanciated object. A short example. May be you
> want to count how many objects are created based on a class. If you want to
> avoid global variables ($), which is a very good decision, you can use class
> variables. Using instance variables will not work.

It will. Note that the poster said "class instance variable" which is
the instance variable of a Class. Classes are instances of Class and
Class inherits from Module which inherits from Object so classes are
objects as well:

--
Wolfgang Nádasi-Donner
"Florian Gross" <> schrieb im Newsbeitrag
news:...
> Wolfgang Nádasi-Donner wrote:
>
> > The class variable ("@@") belongs to the class object, while the
instance
> > variable (@) belongs to the instanciated object. A short example. May be
you
> > want to count how many objects are created based on a class. If you want
to
> > avoid global variables ($), which is a very good decision, you can use
class
> > variables. Using instance variables will not work.
>
> It will. Note that the poster said "class instance variable" which is

I've tested your code and I've been surprised that it actually worked. If I
correctly understand the mechanism, it's like the class object and its instances
are able to access class variables through '@@'. Undubitably, I know now why I
couldn't get along with that feature.

Honestly, I largely prefer to think of classes as objects and use the class
instance variables when I want to share variables between instances. Using '@@'
doesn't seem reliable for me, as IMHO it violates some important OO principles,
the kind that if not followed leads to very embarrassing problems.

I've refactored a bit the code of Wolfgang. Now, it do a lot of reuse through
inheretance. I'm really curious to know what this refactored code would give
under ruby 1.9.0. Would you mind to test it for me, please?

I'd like to take a shot at answering Lionel's original question (what is the
difference between class and instance variables). It seems convenient to use
the result of Guy Decoux's example as an example.

If you use class and instance variables "properly", you can achieve the
following result, which can be very useful in some cases. Note that instance
variables in the dog and cat class count the dogs and cats respectively,
while the class variable (in the animal class) counts the animals (i.e., dogs
+ cats):

0 cats, 0 dogs, 0 animals
a new dog
a new dog
0 cats, 2 dogs, 2 animals
a new cat
a new cat
a new cat
3 cats, 2 dogs, 5 animals

I am a newbie to Ruby, my answer is based on what I understand should happen
with instance and class variables.

On Tuesday 15 March 2005 10:39 am, you wrote:
> If you use class and instance variables "properly", you can achieve the
> following result, which can be very useful in some cases. Note that
> instance variables in the dog and cat class count the dogs and cats

In the previous line, it would have been more clear (and correct ;-) to say
"in the dog and cat *instances* (of the animal class)"

Randy Kramer wrote:
> (Sorry for the top posting.)
>
> I'd like to take a shot at answering Lionel's original question (what is the
> difference between class and instance variables).
Oops, there is some confusion here: "class variable", "class instance variable"
and "instance variable" are three different things.

@a in the context of instance code is an instance variable. "instance variable"
is contained in an object, any object. As being internal to the object, it
cannot be accessed from outside without a method call.

@a in the context of class code is a "class instance variable". "class instance
variable" is contained in an object, but not any one, it is contained in a
class, as classes are objects too. Then, as being internal to the class object,
it cannot be accessed from outside without a class method call.

@@a whatever the context is a "class variable". "class variable" is "contained"
in a class as if it was a "class instance variable" but without being one, it
exists in "another world". In practical terms, the main difference is that '@@'
is reachable from any instance without passing through any method call (which
IMHO is poor OO design).

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!