# Class variables and Constants

**URL:** <https://www.ruby-forum.com/t/class-variables-and-constants/54011>\
**Category:** Ruby\
**Created:** [February 20, 2006, 11:06am UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011 "2006-02-20T11:06:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [February 20, 2006, 11:06am UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011/1 "2006-02-20T11:06:00Z")

</div>

Hello people,

what is the actual difference between class variables and constants?

If I write:

* * *

#!/usr/local/bin/ruby -w

class TonyCantCode  
@@internal=10

```
     def TonyCantCode::something
             return @@internal
     end

     def TonyCantCode::something=(what)
             @@internal=what
     end

```

end

class TonyReallyCantCode  
Something=20  
end

p TonyCantCode::something  
p TonyReallyCantCode::Something

TonyCantCode::something=30  
TonyReallyCantCode::Something=40

## p TonyCantCode::something p TonyReallyCantCode::Something

I get a warning about changing a constant, PLUS I called the method  
TonyCantCode::something so that I didn’t have to use the brackets at  
the end.

BUT… as far the the scope is concerned, they do seem to be very.  
very similar.  
As far as I can see:

- Constants would be referenced mainly from the OUTSIDE of the class.  
For example, if TonyReallycantCode::Something was something very  
meaningful to the USERS of TonyReallyCantCode.

- Class variables would be used mainly from WITHIN the class. Yes, it  
is possible to create accessors, but they might not be necessary.

This is what I worked out. Now the question is: is all the above  
correct?

BYE!

Merc.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [February 20, 2006, 11:36am UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011/2 "2006-02-20T11:36:18Z")

</div>

Dear Tony,

[OK, I am gonna answer myself]

The main difference actually makes a lot of sense.

If you change a class constant (which you shouldn’t do), then all of  
the classes which inherited from it will see their constant changed  
as well.

Class variables, on the other hand, are allocated for the class  
itself. This means that each class (or sub-class) will have its own  
class variables.

Here is some code to clarify the concept.

* * *

#!/usr/local/bin/ruby -w

class TonyCantCode  
@@internal=10

```
     def TonyCantCode::something
             return @@internal
     end

     def TonyCantCode::something=(what)
             @@internal=what
     end

```

end

class TonyReallyCantCode  
Something=20  
end

class TonyCantCode2 \< TonyCantCode  
end

class TonyReallyCantCode2 \< TonyReallyCantCode  
Something=20  
end

p TonyCantCode::something  
p TonyReallyCantCode::Something

TonyCantCode::something=30  
TonyReallyCantCode::Something=40

p TonyCantCode::something  
p TonyReallyCantCode::Something

p TonyCantCode2::something  
p TonyReallyCantCode2::Something

RESULT:

$ ./vars.rb  
10  
20  
./vars.rb:33: warning: already initialized constant Something  
30  
40  
30  
20 \<-- !!!

* * *

Bye!

Merc.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [February 20, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011/3 "2006-02-20T13:37:18Z")

</div>

Hi –

On Mon, 20 Feb 2006, Tony M. wrote:

> means that each class (or sub-class) will have its own class variables.  
> return @@internal  
> Something=20  
> p TonyCantCode::something
> 
> 20 ← !!!  
> Have you tried this?

```
class A
  @@var = 10
  def self.var
    @@var
  end
end

class B < A
  @@var = 20
end

p A.var

```

🙂

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [February 20, 2006, 2:40pm UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011/4 "2006-02-20T14:40:38Z")

</div>

Hi,

> ```
> @@var = 20
> 
> ```
> 
> end
> 
> p A.var
> 
> 🙂

AAAAAAAHHHHHHHHH!!!  
I’ve been playing with this the whole night. I am not gonna stop  
until I’ve finished writing a clear document that explains absolutely  
everything.

Merc.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [February 20, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011/5 "2006-02-20T14:52:54Z")

</div>

Hi –

On Mon, 20 Feb 2006, Tony M. wrote:

> > end  
> > I’ve been playing with this the whole night. I am not gonna stop until I’ve  
> > finished writing a clear document that explains absolutely everything.

I think Matz is planning to change class variables in 2.0 so that they  
are class/module scoped rather than hierarchy scoped. Meanwhile,  
here’s another fun variant:

class A  
end

class B \< A  
@@var = 10  
def self.var  
@@var  
end  
end

class A  
@@var = 20  
def self.var  
@@var  
end  
end

p A.var  
p B.var

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [February 21, 2006, 3:38am UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011/6 "2006-02-21T03:38:33Z")

</div>

DÅ?a Pondelok 20 FebruÃ¡r 2006 14:50 removed\_email\_address@domain.invalid napÃ­sal:

> > AAAAAAAHHHHHHHHH!!!  
> > I’ve been playing with this the whole night. I am not gonna stop until  
> > I’ve finished writing a clear document that explains absolutely  
> > everything.

I tried to do Poor Man’s Traits in Java using static fields. I feel your  
pain.

It’s especially likely to cause initialization timing bugs - when I  
loaded the  
page from the webapp after a server restart, everything worked fine - by  
some  
strange coinkydink, the classes loaded earlier weren’t used after they  
got  
clobbered by the subclasses loaded earlier. Lossage ensued after a page  
reload.

> ```
> @@var
> 
> ```
> 
> p A.var  
> p B.var

Oh dear. I do hope the change gets into 2.0, I can’t wait for Java  
trolls to  
ramble against it…

(Now don’t tell anyone, but I have a hunch class instance variables  
actually  
work like you’d expect in this case. I think.)

David V.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [February 20, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011/7 "2006-02-20T15:20:31Z")

</div>

Hi,

> ```
> @@var
> 
> ```
> 
> end  
> end
> 
> p A.var  
> p B.var

The result is:

20  
10

Oh dear… let me guess.  
To start with, the class A doesn’t have @@var defined. Class B is  
defined, and @@var is allocated. B will share @@var with all of its  
children. Then, A happened to be extended and @@var is allocated. It  
will share @@var with its children, but NOT B.  
The compiler smells something fishy there, and says:

./p.rb:9: warning: class variable @@var of A is overridden by B

Which is what happens: effectively, the hierarchy rule is NOT going  
to be satisfied.

I love Ruby, but this is so error-prone it’s not funny 😑

Merc.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [February 24, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/class-variables-and-constants/54011/8 "2006-02-24T18:45:35Z")

</div>

Hi –

On Tue, 21 Feb 2006, David V. wrote:

> > @@var = 10  
> > end
> > 
> > p A.var  
> > p B.var
> 
> Oh dear. I do hope the change gets into 2.0, I can’t wait for Java trolls to  
> ramble against it…
> 
> (Now don’t tell anyone, but I have a hunch class instance variables actually  
> work like you’d expect in this case. I think.)

Basically a “class instance variable” is no different from any other  
instance variable – it just happens to belong to a class object. The  
phrase has become commonplace, I guess, because it’s sort of unclear  
otherwise, when you talk about, say, “a class’s instance variables”,  
whether you mean the class object’s or the ones it is responsible for  
providing for its own instances.

Anyway, instance variables of class objects do, indeed, provide  
per-class (i.e., per-object-that-happens-to-be-a-class) state, which  
class variables don’t. I think the 2.0 change is going to be that  
class variables will be like instance variables of a class, except  
that they will be visible inside the class’s instance methods.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)
