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><%=@s.increment%></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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Nov-28 20:05 UTC
Re: Class variable does not retain its value in rails
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> Thanks, > Minh T. Nguyen. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung 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. > > Fredoh. then what is the way to do this in ruby? i came from java too, and used the static variable. thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Black wrote:> Frederick Cheung 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 >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thanks... :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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><%=@s.increment%></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 Thomas 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.