Module: BothIsGood::Memoization::ClassMethods
- Defined in:
- lib/both_is_good/memoization.rb
Instance Method Summary collapse
Instance Method Details
#memoize(method_name) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/both_is_good/memoization.rb', line 6 def memoize(method_name) original_method = instance_method(method_name) define_method(method_name) do |*args| raise ArgumentError, "Cannot memoize methods that take arguments" if args.any? @memo ||= {} if @memo.key?(method_name) @memo[method_name] else @memo[method_name] = original_method.bind_call(self) end end end |