Class: Module
- Defined in:
- lib/amp/dependencies/zip/stdrubyext.rb,
lib/amp/support/support.rb
Instance Method Summary collapse
- #forward_message(forwarder, *messagesToForward) ⇒ Object
-
#memoize_method(meth_name, module_function_please = false) ⇒ Object
Makes an instance or module method memoized.
Instance Method Details
#forward_message(forwarder, *messagesToForward) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 99 def (forwarder, *) methodDefs = .map { |msg| "def #{msg}; #{forwarder}(:#{msg}); end" } module_eval(methodDefs.join("\n")) end |
#memoize_method(meth_name, module_function_please = false) ⇒ Object
Makes an instance or module method memoized. Works by aliasing the old method and creating a new one in its place.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/amp/support/support.rb', line 161 def memoize_method(meth_name, module_function_please = false) meth_name = meth_name.to_sym aliased_meth = "__memo_#{meth_name}".to_sym # alias to a new method alias_method aliased_meth, meth_name # module_function the newly aliased method if necessary if module_function_please && self.class == Module module_function aliased_meth end # incase it doesn't exist yet @__memo_cache ||= {} # our new method! Replacing the old one. define_method meth_name do |*args| # we store the memoized data with an i-var. @__memo_cache[meth_name] ||= {} cache = @__memo_cache[meth_name] # if we have the cached value, return it result = cache[args] return result if result # cache miss. find the value result = send(aliased_meth, *args) cache[args] = result result end self end |