Skip to content

Instantly share code, notes, and snippets.

@ruxo
Last active October 2, 2021 02:16
Show Gist options
  • Select an option

  • Save ruxo/92c1f45df28bdb755c0f to your computer and use it in GitHub Desktop.

Select an option

Save ruxo/92c1f45df28bdb755c0f to your computer and use it in GitHub Desktop.
Ruby Memoize
def memoize(method_name)
cache = {}
define_method(method_name) do |*args|
cache[args] ||= yield(*args)
end
end
memoize(:add) {|a,b|
p "calc #{a} and #{b}"
a+b
}
p add(23,45)
p add(23,45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment