On 10/17/05, Brian Buckley <briankbuckley / gmail.com> wrote: > > This is the magic of closures. > > Thank you for the closure explanation. I'll have to experiment. > > It appears then that that cache cannot be a accessed for inspection > (for example to see the history of different calculations made), only > the method created by define_method has use of it. A simple modification to memoize to return the cache would let you see it: module Memoize MEMOIZE_VERSION = "1.0.0" def memoize(name) meth = method(name) cache = {} (class << self; self; end).class_eval do define_method(name) do |*args| cache[args] ||= meth.call(*args) end end cache # <<< add this line end end include Memoize def foo(*args) puts "calculating foo(#{args.map{|x| x.inspect}.join(',')})" args.inject(0) {|sum, x| sum + x} end cache = memoize(:foo) p cache puts foo(2) puts foo(2) puts foo(2,3,4) puts foo(2,3,4) p cache __END__ {} calculating foo(2) 2 2 calculating foo(2,3,4) 9 9 {[2, 3, 4]=>9, [2]=>2} Regards, Sean