From: Frank Tao Date: 2008-12-03T01:27:01+09:00 Subject: dynamic programming # 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 # # So far, I have issues like # 1) dynmically define class variable # 2) failed to include a module inside a method of a class # # Any suggestion or comments will be appreciated # class Adam def self.m_a ["Adam#m_a"] end end class CachedAdam caching_method :adam, :m_a def self.caching_method # this method should do something to make the following codes end end ## CachedAdam#caching_method should make and load the following codes # # module AdamWithCache # def m_a_with_cache # CachedAdam.get_cached_m_a || m_a_without_cache # end # def self.include(base) # base.alias_method_chain :m_a, :cache # end # end # Adam.class_eval{ include AdamWithCache } # # class CachedAdam # def self.get_cached_m_a( adam_id ) # @@cache && @@cache[adam_id] # end # # def self.set_cached_m_a(hash_list) # hash_list.each do |k,v| # @@cache ||={} # failed, any suggestion? # @@cache[k] = v # end # end # end -- Posted via http://www.ruby-forum.com/.