https://github.com/elmassimo/pakiderm
🐘 Pakiderm will never forget the return value
https://github.com/elmassimo/pakiderm
memoization memoize ruby
Last synced: 7 months ago
JSON representation
🐘 Pakiderm will never forget the return value
- Host: GitHub
- URL: https://github.com/elmassimo/pakiderm
- Owner: ElMassimo
- License: mit
- Created: 2014-02-25T19:47:58.000Z (about 12 years ago)
- Default Branch: main
- Last Pushed: 2021-02-24T15:21:55.000Z (about 5 years ago)
- Last Synced: 2025-09-25T06:32:23.946Z (7 months ago)
- Topics: memoization, memoize, ruby
- Language: Ruby
- Homepage:
- Size: 71.3 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Pakiderm [](https://rubygems.org/gems/pakiderm) [](https://github.com/ElMassimo/pakiderm/actions) [](https://codeclimate.com/github/ElMassimo/pakiderm) [](https://github.com/ElMassimo/queryable/blob/master/LICENSE.txt)
=====================
Allows you to unobtrusively memoize methods without parameters.
```ruby
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.
```ruby
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:
```ruby
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.