Method: Module#memoize_function
- Defined in:
- lib/tins/memoize.rb
#memoize_function(*function_ids) ⇒ Object
Automatically memoize calls of the functions function_ids. The memoized result does ONLY depend on the arguments given to the function.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tins/memoize.rb', line 65 def memoize_function(*function_ids) function_ids.extend(ExtractLastArgumentOptions) function_ids, opts = function_ids. mc = __memoize_cache__ function_ids.each do |function_id| function_id = function_id.to_s.to_sym memoize_apply_visibility function_id do orig_function = instance_method(function_id) __send__(:define_method, function_id) do |*args| if mc.key?(function_id) and result = mc[function_id][args] result else (mc[function_id] ||= {})[args] = result = orig_function.bind(self).call(*args) opts[:freeze] and result.freeze $DEBUG and warn "#{self.class} cached function #{function_id}(#{args.inspect unless args.empty?}) = #{result.inspect}" end result end end end function_ids.size == 1 ? function_ids.first : function_ids end |