# Why the class variable doesn't work correctly?

**URL:** <https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618>\
**Category:** Rails\
**Created:** [April 23, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618 "2008-04-23T17:13:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618/1 "2008-04-23T17:13:01Z")

</div>

Hi there, I am new in Ruby.  
I got a question, maybe it’s stupid.  
There is controller named ‘home’, and a action named ‘test’ existed.  
And I defined a class variable named ‘@@total’  
looks like this:

class HomeController \< ApplicationController  
@@total = 0

def test  
@@total += 1  
render :text =\> @@total.to\_s  
end  
end

The logic is very clear and simple, I wanna increase the value of  
@@total when I call it every time.  
So I connect the url: [http://localhost:3000/home/test](http://localhost:3000/home/test) to try it.  
But it ALWAYS responses me “1”, there is no accumulation. What’s going  
on???  
I read the Ruby book and api document, they say the class variable  
should act likes a static variable in Java or C++.  
But I don’t get the result I espected. Could somebody tell me why?  
Thanks!

firestoke

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618/2 "2008-04-23T17:17:46Z")

</div>

On 23 Apr 2008, at 16:12, firestoke wrote:

> def test  
> @@total += 1  
> render :text =\> @@total.to\_s  
> end  
> end
> 
> The logic is very clear and simple, I wanna increase the value of  
> @@total when I call it every time.  
> So I connect the url: [http://localhost:3000/home/test](http://localhost:3000/home/test) to try it.  
> But it ALWAYS responses me “1”, there is no accumulation. What’s going  
> on???

Classes are reloaded between requests in development mode.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618/3 "2008-04-23T17:47:10Z")

</div>

Yup, what Fred said.

Switch to production mode and try it then.

Or, perhaps use the session to store the accumulator. That would be  
per-user session, though.

If you want to persistent, across-the-application accumulator, use a  
file or the DB.

-Danimal

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2008, 8:08am UTC](https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618/4 "2008-04-24T08:08:56Z")

</div>

On Apr 23, 2008, at 10:32 PM, FireStoke wrote:

> Okay, I already tried it and the result is correct now. Thanks!

You shouldn’t depend on it. For instance, if you ever do any sort of  
load balancing your application will break.  
If you need a value to persist, either put it in the session or in the  
database.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2008, 3:48pm UTC](https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618/5 "2008-04-24T15:48:46Z")

</div>

Firestoke,

I’m sure you could, but the question is: why? part of the whole power  
of development mode is the class reloading. That way, as you edit your  
code, you don’t have to constantly restart your server. If you want to  
fiddle without the reloading, why not just stay in production mode?

-Danimal

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 25, 2008, 12:41am UTC](https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618/6 "2008-04-25T00:41:55Z")

</div>

Ok, I see it.  
I will try to save my static data into db.  
Thanks! :^)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2008, 6:32am UTC](https://www.ruby-forum.com/t/why-the-class-variable-doesnt-work-correctly/135618/7 "2008-04-24T06:32:44Z")

</div>

Okay, I already tried it and the result is correct now. Thanks!  
One more question, can I turn off the class auto-reload mechanism in  
development mode?

firestoke
