Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Last active December 12, 2015 07:19
Show Gist options
  • Select an option

  • Save practicingruby/4736138 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/4736138 to your computer and use it in GitHub Desktop.
class ComposedCache
def initialize(target)
@target = target
end
# does not support caching with recursive calls (or calls to other cached methods)
def cache(*method_names)
method_names.each do |m|
results = {}
define_singleton_method(m) do |*a|
results[[m, a]] ||= @target.send(m, *a)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment