Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class variables in Ruby

Reply
Thread Tools

Class variables in Ruby

 
 
Paul
Guest
Posts: n/a
 
      08-18-2006
Hello all,

I am trying to cache lookup table values from a database table into a
hash that is stored in a class variable. This will allow me to read and
store the id/descr value pairs from the database one time only after
which I store them in the hash and retrieve them from there.

The problem is the class variable appears to be cleared out each time a
new request comes in to my controller. I am using rails in the
development enrionment but I am hoping this is a Ruby issue. I thought
maybe rails was reloading the class each time a request comes in
because I read this is standard behavior in the development
environment. However, I believe the reloadable? method is supposed to
care of that?

Here is the code from my model class. The code is alwasy going to super
to read from the database. If anyone can steer me straight on this I
would appreciate it!

Thanks in advance.
Paul

class GrammarTenseCode < ActiveRecord::Base
has_many :words
@@cachedDescr = {}

def self.reloadable?
false
end

def descr
if @@cachedDescr[id.to_s] == nil
@@cachedDescr[id.to_s] = super
else
@@cachedDescr[id.to_s]
end
end
end

 
Reply With Quote
 
 
 
 
khaines@enigo.com
Guest
Posts: n/a
 
      08-18-2006
On Sat, 19 Aug 2006, Paul wrote:

> @@cachedDescr = {}


If you think reloading is happening, try:

@@cacheDescr = {} unless @@cacheDescr


Kirk Haines


 
Reply With Quote
 
 
 
 
Daniel Waite
Guest
Posts: n/a
 
      08-18-2006
> @@cacheDescr = {} unless @@cacheDescr

Or, to be more idiomatic:

@@cache_descr ||= {}



--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
khaines@enigo.com
Guest
Posts: n/a
 
      08-18-2006
On Sat, 19 Aug 2006, Daniel Waite wrote:

>> @@cacheDescr = {} unless @@cacheDescr

>
> Or, to be more idiomatic:
>
> @@cache_descr ||= {}


Which I usually prefer, but the variant that uses unless reads more like a
sentence if a person isn't sure what's happening there.

I wish I could use a simple structure like that for constants.

Foo = 7 unless const_defined? :Foo

is wordier than I like.


Kirk Haines

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      08-18-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Sat, 19 Aug 2006, Paul wrote:
>
> > @@cachedDescr = {}

>
> If you think reloading is happening, try:
>
> @@cacheDescr = {} unless @@cacheDescr
>
>
> Kirk Haines


Hi Kirk,

I tried @@cacheDescr = {} unless @@cacheDescr but I get the same
results. I would expect the initialization of @@cacheDescr to take
place when the .rb package is loaded. I will keep playing with it and
post a solution when I figure it out.

Thanks,
Paul

 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      08-19-2006
On Sat, 19 Aug 2006, Paul wrote:

>
> (E-Mail Removed) wrote:
>> On Sat, 19 Aug 2006, Paul wrote:
>>
>>> @@cachedDescr = {}

>>
>> If you think reloading is happening, try:
>>
>> @@cacheDescr = {} unless @@cacheDescr
>>
>>
>> Kirk Haines

>
> Hi Kirk,
>
> I tried @@cacheDescr = {} unless @@cacheDescr but I get the same results. I
> would expect the initialization of @@cacheDescr to take place when the .rb
> package is loaded. I will keep playing with it and post a solution when I
> figure it out.


but the file may be getting loaded each time... depending on mode. fyi.

-a
--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dali lama

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      08-19-2006
SOLUTION: Rails, in the development environment, was reloading the
class for every request coming into the application and therefore
causing the Class variable to be reset. Something I read led me to
believe I could add the following method to my class to cause it not to
be reloaded

def self.reloadable?
false
end

That was false. To keep rails from reloading the class every time you
can change the ..\config\environments\development.rd file. It has a
"config.cache_classes = false" line that you can change to <true>
which will prevent the classes from being reloaded. You are REQUIRED to
stop the WEBrick server though to pick up this change as well as
whenever to want to pick up any recent coding changes. BUT, you can
change this parameter temporarily to test out Class variables that are
expected to store data throughout your session. Don't forget to change
it back to <false> to be back in the default mode which is the
desirable mode for development.

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      08-19-2006
On Aug 18, 2006, at 9:05 PM, Paul wrote:

> To keep rails from reloading the class every time you
> can change the ..\config\environments\development.rd file.


You really, really don't want to do this. Trust me.

You've already noted that this makes development mode suck. If you
go this way, you will need to toggle the setting every time you need
it to work or go without Rails's best development feature. Ouch.

That should be reason enough, but it gets worse!

Depending on how you deploy your application, it's quite likely you
could have two or more separate interpreters running your production
code. These will *not* share class variables. This is the way of
pain and could be a source of all kinds of nasty issues.

If you need to remember a value in Rails it needs to be in the
database or the session.

James Edward Gray II


 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      08-19-2006
On Sat, 19 Aug 2006, James Edward Gray II wrote:

> If you need to remember a value in Rails it needs to be in the database or
> the session.


or you need to configure your app so that only one instance will run at once -
assuming fcgi execution. this is actually an easy solution for some simple
apps. however, james is quite correct.

-a
--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dali lama

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      08-19-2006
James,

Thanks for the comments. I am coming from a Smalltalk background and we
would typically cache code/description lookup table rows into a class
variable for performance. That is, when we wanted to look up a
description we would check the cache first and then hit the database
only if it had not been retrieved yet.

In this scenario, it's not a problem if the cache is maintained
individually over separate instances of the interpreter. This way every
user benefits from the cache rather than storing it in a session unless
I misunderstand the life cycle of class variables. I would think they
would live until the class is reloaded.

However, I do see your point and agree and that under most application
situations this would not be the way to go.

Thanks,
Paul


James Edward Gray II wrote:
> On Aug 18, 2006, at 9:05 PM, Paul wrote:
>
> > To keep rails from reloading the class every time you
> > can change the ..\config\environments\development.rd file.

>
> You really, really don't want to do this. Trust me.
>
> You've already noted that this makes development mode suck. If you
> go this way, you will need to toggle the setting every time you need
> it to work or go without Rails's best development feature. Ouch.
>
> That should be reason enough, but it gets worse!
>
> Depending on how you deploy your application, it's quite likely you
> could have two or more separate interpreters running your production
> code. These will *not* share class variables. This is the way of
> pain and could be a source of all kinds of nasty issues.
>
> If you need to remember a value in Rails it needs to be in the
> database or the session.
>
> James Edward Gray II


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class variables, instance variables, singleton; Ruby v. C++ Ralph Shnelvar Ruby 29 11-30-2009 07:43 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Accessing class member variables - properties or variables? dwok Java 7 03-04-2005 03:54 AM
Re: Class public shared Variables vs. Application Variables in ASP.NET avnrao ASP .Net 0 05-07-2004 05:28 AM



Advertisments