Skip to content

Instantly share code, notes, and snippets.

@ddollar
Created June 24, 2009 20:08
Show Gist options
  • Select an option

  • Save ddollar/135492 to your computer and use it in GitHub Desktop.

Select an option

Save ddollar/135492 to your computer and use it in GitHub Desktop.
Fibonacci
def fib(n)
return n if n < 2
fib(n-1) + fib(n-2)
end
def fib_with_caching(n)
@fib_cache ||= {}
@fib_cache[n] ||= fib_without_caching(n)
end
alias_method_chain :fib, :caching
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment