# Class Variable Scope

**URL:** <https://www.ruby-forum.com/t/class-variable-scope/80174>\
**Category:** Rails\
**Created:** [December 29, 2005, 7:54am UTC](https://www.ruby-forum.com/t/class-variable-scope/80174 "2005-12-29T07:54:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [December 29, 2005, 7:54am UTC](https://www.ruby-forum.com/t/class-variable-scope/80174/1 "2005-12-29T07:54:56Z")

</div>

(Hopefully) a quick question about variable scope issues. In one of my  
controllers, I have a class variable:

class TestController \< ApplicationController  
@@testClassVariable = 0  
…

The problem is, in my “list” method I’m trying to increment this  
variable by one each time, printing it out, but it seems to reset to 0  
every time the list method is called:

def list  
…  
@@testClassVariable += 1  
logger.info @@testClassVariable #prints “1” each time  
…  
end

All the examples I’ve found online tell me this should work, printing  
out incrementally higher numbers. Do I have a problem with my scope  
that I’m just not understanding? Or are controller classes special  
beasts? Any clarification would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [December 29, 2005, 9:16am UTC](https://www.ruby-forum.com/t/class-variable-scope/80174/2 "2005-12-29T09:16:13Z")

</div>

If you’re running in development mode, your classes are being reloaded  
(effectively ‘reset’) for every request… you might instead want to  
consider storing this information in the session hash, or even in the  
database itself.

- james

---

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [December 29, 2005, 8:04pm UTC](https://www.ruby-forum.com/t/class-variable-scope/80174/3 "2005-12-29T20:04:38Z")

</div>

Thank you for both your replies! Jame’s comment about running in  
development mode is exactly right I think, and storing the variables in  
the session sounds like a great solution. Thanks again!

---

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [December 29, 2005, 4:46pm UTC](https://www.ruby-forum.com/t/class-variable-scope/80174/4 "2005-12-29T16:46:30Z")

</div>

On 12/29/05, Ben CH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> beasts? Any clarification would be greatly appreciated!

One possible ‘hack-around’ would be to say:  
@@testClassVariable ||= 0  
instead of  
@@testClassVariable = 0

…which means “Assign 0 to @@testClassVariable unless it already has a  
value.”

James’s suggestion of the session or database is definitely more robust,  
though.
