class PostOfTheDay
private
def self.current_day
DateTime.now.utc.to_date
end
@@current_post = nil
@@current_day = current_day
def self.get_random_post
posts = Post.all
posts[rand(posts.size)]
end
public
def self.fetch
if @@current_post.nil? || @@current_day != current_day
@@current_post = get_random_post
@@current_day = current_day
end
return @@current_post
end
end
I would expect the class variables @@current_post and @@current_day to never
change when not accessed. However, in development mode, on each request, a
different Post is returned because either the Klazz has been garbage
collected (?) or the initializers
@@current_post = nil
@@current_day = current_day
have been re-run. But isn''t it required to initialize a class variable
before usage?
Why is this the default behaviour? Any pointers on a different approach?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On Apr 27, 11:49 pm, Commander Johnson <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class PostOfTheDay > private > > def self.current_day > DateTime.now.utc.to_date > end > > @@current_post = nil > @@current_day = current_day > > def self.get_random_post > posts = Post.all > > posts[rand(posts.size)] > end > > public > > def self.fetch > if @@current_post.nil? || @@current_day != current_day > @@current_post = get_random_post > @@current_day = current_day > end > > return @@current_post > end > end > > I would expect the class variables @@current_post and @@current_day to never > change when not accessed. However, in development mode, on each request, a > different Post is returned because either the Klazz has been garbage > collected (?) or the initializers >In development mode classes are cleaned out and loaded from scratch on each request (this is why you don''t need to restart the server to see changes. In production this isn''t true. Fred> @@current_post = nil > @@current_day = current_day > > have been re-run. But isn''t it required to initialize a class variable > before usage? > > Why is this the default behaviour? Any pointers on a different approach?
Thanks, I just read http://www.ruby-forum.com/topic/133234 which is also very clear. I''ll just store that Post of the Day inside an ActiveRecord model. On Tue, Apr 28, 2009 at 8:59 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Apr 27, 11:49 pm, Commander Johnson <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > class PostOfTheDay > > private > > > > def self.current_day > > DateTime.now.utc.to_date > > end > > > > @@current_post = nil > > @@current_day = current_day > > > > def self.get_random_post > > posts = Post.all > > > > posts[rand(posts.size)] > > end > > > > public > > > > def self.fetch > > if @@current_post.nil? || @@current_day != current_day > > @@current_post = get_random_post > > @@current_day = current_day > > end > > > > return @@current_post > > end > > end > > > > I would expect the class variables @@current_post and @@current_day to > never > > change when not accessed. However, in development mode, on each request, > a > > different Post is returned because either the Klazz has been garbage > > collected (?) or the initializers > > > In development mode classes are cleaned out and loaded from scratch on > each request (this is why you don''t need to restart the server to see > changes. In production this isn''t true. > > Fred > > > @@current_post = nil > > @@current_day = current_day > > > > have been re-run. But isn''t it required to initialize a class variable > > before usage? > > > > Why is this the default behaviour? Any pointers on a different approach? > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---