Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/engtagger.rb

Overview

for memoization (code snipet from eigenclass.org/hiki/bounded-space-memoization)

Instance Method Summary collapse

Instance Method Details

#memoize(method) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/engtagger.rb', line 23

def memoize(method)
  # alias_method is faster than define_method + old.bind(self).call
  alias_method "__memoized__#{method}", method
  module_eval <<-EOF
    def #{method}(*a, &b)
      # assumes the block won't change the result if the args are the same
      (@__memoized_#{method}_cache ||= {})[a] ||= __memoized__#{method}(*a, &b)
    end
  EOF
end