Last active
August 29, 2015 14:07
-
-
Save duairc/3a01bb861abc4646e4c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Method | |
def memoize!(time = nil) | |
name = self.name | |
owner = self.owner | |
method = self.to_proc | |
self.unbind | |
owner.send(:define_method, name) do |*args, &block| | |
@results ||= {} | |
if !@time || (time && @time + time < Time.now) | |
result = method.call(*args, &block) | |
@time = Time.now | |
@results[[args, block]] = result | |
end | |
@results[[args, block]] | |
end | |
owner.method(name) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment