ruxo
2/5/2016 - 5:00 AM

Ruby Memoize

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)