Skip to content

Instantly share code, notes, and snippets.

@bfabry
Created February 8, 2013 00:55
Show Gist options
  • Save bfabry/4735688 to your computer and use it in GitHub Desktop.
Save bfabry/4735688 to your computer and use it in GitHub Desktop.
class Memoizer
def initialize(tgt)
@target = tgt
@cache = {}
end
def cache(*method_names)
method_names.each {|name| cache[name] = {} }
end
def method_missing(m, *a, &b)
if @cache[m]
@cache[m][a] ||= @target.send(m, *a, &b)
else
@target.send(m, *a, &b)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment