# Class variables and modules

**URL:** <https://www.ruby-forum.com/t/class-variables-and-modules/189669>\
**Category:** Ruby\
**Created:** [June 4, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/class-variables-and-modules/189669 "2010-06-04T19:26:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [June 4, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/class-variables-and-modules/189669/1 "2010-06-04T19:26:48Z")

</div>

I am trying to keep a history of object state in a parent class using a  
class variable. This works per example 1 using child method  
update\_state. but if i mixin a module to update\_state (so as not to  
repeat update\_state every), per example 2 i get different behavior. i do  
not understand why.  
########################### example 1 ########################  
class A  
def initialize  
@@history= []  
@@incoming\_state = [‘initial incoming state’]  
end

def update\_history  
@@history = @@history.push( @@incoming\_state.flatten)  
end

def history  
@@history  
end  
end

class B \< A  
def new\_state  
@another\_state = [“new state”]  
end  
attr :another\_state  
def update\_state  
@@incoming\_state = self.another\_state  
end  
end  
a = A.new  
b=B.new  
b.new\_state  
b.update\_state  
b.update\_history  
b.update\_state  
b.update\_history  
p b.history  
################################ example 2  
################################

class A  
def initialize  
@@history= []  
@@incoming\_state = [‘initial incoming state’]  
end

def update\_history  
@@history = @@history.push( @@incoming\_state.flatten)  
end

def history  
@@history  
end  
end

module Keeper  
def update\_state  
@@incoming\_state = self.another\_state  
end  
end

class B \< A  
include Keeper  
def new\_state  
@another\_state = [“new state”]  
end  
attr :another\_state  
end

a = A.new  
b=B.new  
b.new\_state  
b.update\_state  
b.update\_history  
b.update\_state  
b.update\_history  
p b.history

---

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [June 4, 2010, 10:23pm UTC](https://www.ruby-forum.com/t/class-variables-and-modules/189669/2 "2010-06-04T22:23:42Z")

</div>

On Fri, Jun 4, 2010 at 7:26 PM, Ill E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Short answer: Do not use class variables, period! Because they are  
shared amongst subclasses.  
Long answer: Ok that’s exactly what you want… include Keeper into A  
and leave the subclasses alone 😉

---

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [June 4, 2010, 11:09pm UTC](https://www.ruby-forum.com/t/class-variables-and-modules/189669/3 "2010-06-04T23:09:22Z")

</div>

Robert D. wrote:

> On Fri, Jun 4, 2010 at 7:26 PM, Ill E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> Short answer: Do not use class variables, period! Because they are  
> shared amongst subclasses.  
> Long answer: Ok that’s exactly what you want… include Keeper into A  
> and leave the subclasses alone 😉

Much thanks from illeverbe

---

<div class="post-metadata">

**Author:** ![illeverbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@illeverbe](https://www.ruby-forum.com/u/illeverbe)\
**Post date:** [June 5, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/class-variables-and-modules/189669/4 "2010-06-05T21:20:26Z")

</div>

As a side note:

On 2010-06-04 10:26:50 -0700, Ill E. said:

> def update\_history  
> @@history = @@history.push( @@incoming\_state.flatten)  
> end

Array#push modifies the receiver, so you can simply say:

def update\_history  
@@history.push(@@incoming\_state.flatten)  
end

or even:

@@history \<\< @@incoming\_state.flatten
