Sean O'Halpin wrote: > 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 I'm debating between this suggestion and Pit's (where you access the cache via it's name and as an instance method). I dunno - what do people prefer? This one is certainly simpler :) Regards, Dan