Class: Module
Class Method Summary collapse
-
.__memoize_cache__ ⇒ Object
Returns the current memoize cache for all the stored objects and method call results.
-
.__memoize_cache_delete__ ⇒ Object
Finalizer to delete the stored result for a garbage collected object.
Instance Method Summary collapse
-
#__memoize_cache__ ⇒ Object
Returns the current memoize cache for this Module.
-
#memoize_cache_clear ⇒ Object
Clear cached values for all methods and functions.
-
#memoize_function(*function_ids) ⇒ Object
Automatically memoize calls of the functions
function_ids
. -
#memoize_method(*method_ids) ⇒ Object
Automatically memoize calls of the the methods
method_ids
. - #named(name, method, *args, &block) ⇒ Object
Class Method Details
.__memoize_cache__ ⇒ Object
Returns the current memoize cache for all the stored objects and method call results.
7 8 9 |
# File 'lib/tins/memoize.rb', line 7 def __memoize_cache__ @__memoize_cache__ ||= {} end |
.__memoize_cache_delete__ ⇒ Object
Finalizer to delete the stored result for a garbage collected object.
12 13 14 15 16 17 |
# File 'lib/tins/memoize.rb', line 12 def __memoize_cache_delete__ lambda do |id| $DEBUG and warn "Deleted method results for object id='#{id}'" __memoize_cache__.delete(id) end end |
Instance Method Details
#__memoize_cache__ ⇒ Object
Returns the current memoize cache for this Module.
44 45 46 |
# File 'lib/tins/memoize.rb', line 44 def __memoize_cache__ @__memoize_cache__ end |
#memoize_cache_clear ⇒ Object
Clear cached values for all methods and functions.
69 70 71 72 73 |
# File 'lib/tins/memoize.rb', line 69 def memoize_cache_clear mc = @__memoize_cache__ and mc.clear mc = ::Module.__memoize_cache__ and mc.clear self end |
#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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tins/memoize.rb', line 51 def memoize_function(*function_ids) mc = @__memoize_cache__ ||= {} function_ids.each do |method_id| method_id = method_id.to_s.to_sym orig_method = instance_method(method_id) __send__(:define_method, method_id) do |*args| if mc.key?(method_id) and result = mc[method_id][args] result else (mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args) $DEBUG and warn "#{self.class} cached function #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect}" end result end end end |
#memoize_method(*method_ids) ⇒ Object
Automatically memoize calls of the the methods method_ids
. The memoized results do NOT ONLY depend on the arguments, but ALSO on the object the method is called on.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tins/memoize.rb', line 23 def memoize_method(*method_ids) method_ids.each do |method_id| method_id = method_id.to_s.to_sym orig_method = instance_method(method_id) __send__(:define_method, method_id) do |*args| unless mc = ::Module.__memoize_cache__[__id__] mc = ::Module.__memoize_cache__[__id__] ||= {} ObjectSpace.define_finalizer(self, ::Module.__memoize_cache_delete__) end if mc.key?(method_id) and result = mc[method_id][args] result else (mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args) $DEBUG and warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]" end result end end end |