Skip to content

Instantly share code, notes, and snippets.

@duairc
Last active August 29, 2015 14:07
Show Gist options
  • Save duairc/3a01bb861abc4646e4c6 to your computer and use it in GitHub Desktop.
Save duairc/3a01bb861abc4646e4c6 to your computer and use it in GitHub Desktop.
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