Skip to content

Instantly share code, notes, and snippets.

Created July 11, 2016 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e459db5abe5ae2528b724db134930ad5 to your computer and use it in GitHub Desktop.
Save anonymous/e459db5abe5ae2528b724db134930ad5 to your computer and use it in GitHub Desktop.
def createCached meth
cache = {}
Proc.new do |*args|
puts args
puts cache
if cached = cache[args]
cached
else
cache[args] = meth.call(*args)
end
end
end
def fib n
return 1 if n < 2
fib(n-1)+fib(n-2)
end
self.send(:define_singleton_method,:fib,createCached(method(:fib)))
(0..500).map { |y| method(:fib).call(y) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment