From: Rick DeNatale Date: 2008-07-14T23:47:16+09:00 Subject: Re: Help with class instance variables ------=_Part_55115_763115.1216047108000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Mon, Jul 14, 2008 at 7:58 AM, Ittay Dror wrote: > Hi, > > Rick Denatale wrote: > > > You haven't used an class variables on this thread. Your initial code > > created class instance variables, i.e. instance variables belonging to > > the > > object which represents the class. Class variables in Ruby use a double > > @ > > prefix, are visible/shared between the class, any subclasses, and > > instances > > of the same, have some interesting quirks in their semantics and are > > avoided > > or at least used sparingly by many experienced Rubyists. > > > > > > Let me rephrase, trying to to use any terminology. Let's say I have a > class Person whose instances have variables like name and age. Now, the > Person class also has a cache for all persons, loaded from a file. So > this is a single variable shared by all instances and is initialized > once. How do I initialize it in Ruby? > Well, it depends on just what you are doing with the cache. class Person def self.setup_cache @cache = #... whatever you do to initialize the cache end setup_cache # Now assuming we've got some kind of key to access the cache we probably want a class method to get # cached people def self.person(cache_key) @cache[cache_key] end end -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------=_Part_55115_763115.1216047108000--