Skip to content

Instantly share code, notes, and snippets.

Created July 11, 2016 13:51
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/d311143ab38563811e5584346c9a2a36 to your computer and use it in GitHub Desktop.
Save anonymous/d311143ab38563811e5584346c9a2a36 to your computer and use it in GitHub Desktop.
def createCached meth
m = Module.new do
@cache = {}
def self.x args
p @cache
unless @cache[args]
@cache[args] = @m.call(*args)
end
end
end
m.instance_exec(meth) { @m = meth}
m.method(:x)
end
def fib n
return n if n < 2
fib(n-1)+fib(n-2)
end
x = createHashed method(:fib)
(0..39).map { |y| x.call y }
(0..39).map { |y| x.call y }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment