Module: MonkeySupport::Memoizable

Included in:
ActiveSupport::Inflector
Defined in:
lib/monkeysupport/memoizable.rb

Instance Method Summary collapse

Instance Method Details

#monkey_memoize(*methods) ⇒ Object

This is faster than AS::Memoizeable. Less featureful, however.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/monkeysupport/memoizable.rb', line 5

def monkey_memoize(*methods)
  methods.each do |method|
    class_eval <<EOS

@__#{method} = {}
alias_method :__#{method}, :#{method}

if method(:#{method}).arity == 1
      
  def #{method}(arg)
@__#{method}[arg] ||= __#{method}(arg)
  end
  
else
  
  def #{method}(*args)
@__#{method}[args] ||= __#{method}(*args)
  end
  
end

EOS
  
  end
end