From: Jim Weirich Date: 2004-04-10T05:32:56+09:00 Subject: Re: Cache of objects rolo wrote: >>>class Thing >>> @cache = {} >>> def self.instance(name) >>> @cache[name] ||= new(name) >>> end >>> def initialize(name) >>> @name = name >>> end >>>end > > [rolo] There are 2 things here. one Thing object and other t1 object. > @cache is a variable that belongs to Thing or t1? @cache belongs to the Thing object. Notice that is is defined in the class definition of Thing without being nested in a method definition. And it is referenced in a class method (def self.instance). -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)