Pakiderm
Allows you to unobtrusively memoize methods without parameters.
class Stopwatch
extend Pakiderm
def start
@started_at = Time.now
end
def stop
elapsed_time
end
memoize \
def elapsed_time
Time.now - @started_at
end
end
Memoized methods will only be called the first time; the result will be stored and returned on subsequent invocations.
stopwatch = Stopwatch.new
stopwatch.start
stopwatch.stop # Example: 3.991826
stopwatch.elapsed_time # => 3.991826
You can also pass a list of methods that should be memoized:
memoize :complex_calculation, :api_response, :db_query
Background
Pakiderm adds a method by using Module#prepend
in order to"intercept" calls to the original method and provide memoization.
Caveat
If you override the method in a subclass, Pakiderm's method won't be able to intercept calls to the subclass' method, so you would need to call memoize
again in the subclass.