Class variable does not retain its value in rails

Howdy,

Newbie question: how come my class variable (“static” in Java/C#)
doesn’t retain its value in Rails? The following code works well in the
Ruby console (the number is incremented all the time even though new
instances are created), but in Rails it always restarts at 1.

In the /libs/ folder:

class StaticTest
@@call_count = 0

 def increment
   @@call_count += 1
   "@@call_count incremented to " + @@call_count.to_s
 end

end

In the controller:

def index
  @s = StaticTest.new
end

On the view:

<h1><%[email protected]%></h1>

Could it be that in Rails every single time the garbage collector cleans
up the class? I understand that controllers are instantiated on every
request, but I would expect my class variable @@call_count to retain its
value over requests, no?

Thanks,
Minh T. Nguyen.

On 28 Nov 2007, at 19:24, Minh Nguyen wrote:

Could it be that in Rails every single time the garbage collector
cleans
up the class? I understand that controllers are instantiated on every
request, but I would expect my class variable @@call_count to retain
its
value over requests, no?

In development mode your classes are reloaded for every request (so
that you changes to your code are picked up without a server restart).
In production mode each mongrel acts independently, so don’t rely on
this sort of stuff other than as a cache.

Fred

Frederick C. wrote:

On 28 Nov 2007, at 19:24, Minh Nguyen wrote:

Could it be that in Rails every single time the garbage collector
cleans
up the class? I understand that controllers are instantiated on every
request, but I would expect my class variable @@call_count to retain
its
value over requests, no?

In development mode your classes are reloaded for every request (so
that you changes to your code are picked up without a server restart).
In production mode each mongrel acts independently, so don’t rely on
this sort of stuff other than as a cache.

Fred

oh. then what is the way to do this in ruby? i came from java too, and
used the static variable.

thanks

Well, in production mode it would work similar to C++ / Java. As long as
the app is running, the value will be static. Just remember that it is
per server instance as well and will be erased if the server is
restarted. If you want something to be truly persistent, store it in a
persistent store like a database.

-Bill

Trent B. wrote:

its

oh. then what is the way to do this in ruby? i came from java too, and
used the static variable.

thanks


Sincerely,

William P.

thanks… :slight_smile:

Minh Nguyen wrote:

Howdy,

Newbie question: how come my class variable (“static” in Java/C#)
doesn’t retain its value in Rails? The following code works well in the
Ruby console (the number is incremented all the time even though new
instances are created), but in Rails it always restarts at 1.

In the /libs/ folder:

class StaticTest
@@call_count = 0

 def increment
   @@call_count += 1
   "@@call_count incremented to " + @@call_count.to_s
 end

end

In the controller:

def index
  @s = StaticTest.new
end

On the view:

<h1><%[email protected]%></h1>

Besides the suggestion to store it in the db (which is the best one),
please be aware that your current implementation is not thread safe. If
you insist on using class variables (which should always be your last
resort), at least make sure they are synchronized or you will stay up to
3:00 am debugging your app… Sadly speaking from experience…

Dave T. has a great section on thread safety within the Pick-Axe
book which you can see for free online (the 1.6 version)

hth

ilan

The way I found to work arround this is to use the Thread.main:

def init
if Thread.main[:uuid] == nil
Thread.main[:uuid]=0
end
uid = Thread.main[:uuid] += 1
render :json => uid
end

http://giladmanor.com/2010/10/rails-application-variable.html

Gilad

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs