Chuck Remes
10/13/2008 7:34:00 PM
On Oct 13, 2008, at 2:17 PM, matt neuburg wrote:
> [snip]
>
> [The actual question:]
>
> There's one thing happening here I don't understand. Let's say you
> don't
> supply a value for the "cache" parameter. So the cache is simply
> Hash.new. But where does this Hash.new live? It isn't assigned to a
> variable name so what keeps it alive? It works, but how?
This line here keeps the reference to cache alive:
define_method(name) { |*args| cache[args] ||= send(original, *args) }
This is defining a new method and knows to return the cached value or
send the arguments to the original unmemoized method so it can compute
your value.
cr