From: Ron Fox Date: 2008-12-04T01:04:38+09:00 Subject: Re: dynamic programming Brian Candler wrote: > Frank Tao wrote: >> # I have a class Adam >> # I want to modify the method("m_a") so that it will return the cached >> result >> # If no cached result is available, then return the original result >> # I want to create a class method(AKA: macro) to make it DRY > > You might want to look at the 'memoize' method in ActiveRecord trunk. > >> # So far, I have issues like >> # 1) dynmically define class variable > > I suggest: don't use a class variable :-) An instance variable of the > class would be fine. But personally I wouldn't keep the memoized values > in the class; I'd keep them in the instances themselves. > I suggest: don't make blanket statements like that ;-) That depends on what he wants the cache to accomplish. If the cache should cache across all instances of Adam, a class variable is _exactly_ what he wants. If it should only cache for the specific instance of Adam an instance variable is what he wants. If there will be many instances of Adam, and if Adam can peform operations that invalidate cache then a global cache is so much easier to handle than one distributed over all instances of Adam...since two instances of adam may have the same item in their caches, and if instance one invalidates.. how do you find/invalidate the item in instance 2? RF -- Ron Fox NSCL Michigan State University East Lansing, MI 48824-1321