# Class Variables for subclases

**URL:** <https://www.ruby-forum.com/t/class-variables-for-subclases/90260>\
**Category:** Ruby\
**Created:** [March 27, 2007, 4:37am UTC](https://www.ruby-forum.com/t/class-variables-for-subclases/90260 "2007-03-27T04:37:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [March 27, 2007, 4:37am UTC](https://www.ruby-forum.com/t/class-variables-for-subclases/90260/1 "2007-03-27T04:37:36Z")

</div>

Hey Guys,

Check out the following:

class A  
@@anobject = ‘green’  
def fun2  
@@anobject  
end  
end

class B \< A  
@@anobject = ‘blue’  
def fun2  
@@anobject  
end  
end

puts B.new.fun2 # blue  
puts A.new.fun2 # blue

So it looks like the subclass B is not defining its own class variable  
but reusing A’s class variable @@anobject. Is there a different way to  
specify class variables to overcome this issue?

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [March 27, 2007, 4:44am UTC](https://www.ruby-forum.com/t/class-variables-for-subclases/90260/2 "2007-03-27T04:44:37Z")

</div>

Hi,

In message “Re: Class Variables for subclases…”  
on Tue, 27 Mar 2007 11:37:38 +0900, Sonny C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:  
|Check out the following:  
|  
|class A  
| @@anobject = ‘green’  
| def fun2  
| @@anobject  
| end  
|end  
|  
|class B \< A  
| @@anobject = ‘blue’  
| def fun2  
| @@anobject  
| end  
|end  
|  
|puts B.new.fun2 # blue  
|puts A.new.fun2 # blue  
|  
|So it looks like the subclass B is not defining its own class variable  
|but reusing A’s class variable @@anobject. Is there a different way to  
|specify class variables to overcome this issue?

You can use constants-as-class-shared technique.

class A  
Anobject = [‘green’]  
def fun2  
Anobject[0]  
end  
end

class B \< A  
Anobject = [‘blue’]  
def fun2  
Anobject[0]  
end  
end

puts B.new.fun2 # blue  
puts A.new.fun2 # blue

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [March 27, 2007, 5:25am UTC](https://www.ruby-forum.com/t/class-variables-for-subclases/90260/3 "2007-03-27T05:25:21Z")

</div>

Yukihiro M. wrote:

> You can use constants-as-class-shared technique.

Thanks, Matz. For my edification, is there a reason why class variables  
behave in this way when subclassed? Is this a feature of the language  
or the Ruby Interpretter?

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [March 27, 2007, 5:34am UTC](https://www.ruby-forum.com/t/class-variables-for-subclases/90260/4 "2007-03-27T05:34:39Z")

</div>

Sonny C. wrote:

> So it looks like the subclass B is not defining its own class variable  
> but reusing A’s class variable @@anobject. Is there a different way to  
> specify class variables to overcome this issue?
> 
> Sonny.

You may find it more suitable to use instance variables in your class  
objects, and write accessors so you can get at them from instances of  
your classes.

For example:

class A  
@anobject = ‘green’  
def self.anobject  
@anobject  
end  
def fun2  
self.class.anobject  
end  
end

class B \< A  
@anobject = ‘blue’  
end

puts B.new.fun2 # blue  
puts A.new.fun2 # green

Google (or search the list) for ‘ruby class instance variable’.

I think the ‘traits’ gem does this in a systematic way, though of course  
it’s worth playing around to understand the concept first.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [March 27, 2007, 5:38am UTC](https://www.ruby-forum.com/t/class-variables-for-subclases/90260/5 "2007-03-27T05:38:54Z")

</div>

Thanks for the pointers Joel.

Sonny.
