https://github.com/makandra/memoized
Memoize your methods.
https://github.com/makandra/memoized
Last synced: 7 months ago
JSON representation
Memoize your methods.
- Host: GitHub
- URL: https://github.com/makandra/memoized
- Owner: makandra
- License: other
- Created: 2019-02-18T18:16:38.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-13T15:37:33.000Z (almost 2 years ago)
- Last Synced: 2024-11-07T16:18:28.038Z (7 months ago)
- Language: Ruby
- Homepage: https://makandra.com
- Size: 55.7 KB
- Stars: 13
- Watchers: 11
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Memoized [](https://github.com/makandra/memoized/actions)
Memoized will memoize the results of your methods. It acts much like
`ActiveSupport::Memoizable` without all of that freezing business. The API for
unmemoizing is also a bit more explicit.## Install
```
$ gem install memoized
```## Usage
To define a memoized instance method, use `memoize def`:
```ruby
class A
include Memoizedmemoize def hello
'hello!'
end
end
```You may also `memoize` one or more methods after they have been defined:
```ruby
class B
include Memoizeddef hello
'hello!'
enddef goodbye
'goodbye :('
endmemoize :hello, :goodbye
end
```Memoizing class methods works the same way:
```ruby
class C
class << self
include Memoizedmemoize def hello
'hello!'
end
end
end
```To unmemoize a specific method:
```ruby
instance = A.new
instance.hello # the hello method is now memoized
instance.unmemoize(:hello) # the hello method is no longer memoized
instance.hello # the hello method is run again and re-memoized
```To unmemoize all methods for an instance:
```ruby
instance = B.new
instance.hello # the hello method is now memoized
instance.goodbye # the goodbye method is now memoized
instance.unmemoize_all # neither hello nor goodbye are memoized anymore
```## Limitations
When you are using Memoized with default arguments or default keyword arguments, there are some edge cased you have to
keep in mind.When you memoize a method with (keyword) arguments that have an expression as default value, you should be aware
that the expression is evaluated only once.```ruby
memoize def print_time(time = Time.now)
time
endprint_time
=> 2021-07-23 14:23:18 +0200sleep(1.minute)
print_time
=> 2021-07-23 14:23:18 +0200
```When you memoize a method with (keyword) arguments that have default values, you should be aware that Memoized
differentiates between a method call without arguments and the default values.```ruby
def true_or_false(default = true)
puts 'calculate value ...'
default
endtrue_or_false
calculate value ...
=> truetrue_or_false
=> truetrue_or_false(true)
calculate value ...
=> true
```## Development
Development
-----------I'm very eager to keep this gem leightweight and on topic. If you're unsure whether a change would make
it into the gem, [talk to me beforehand](mailto:[email protected]).There are tests in `spec`. We only accept PRs with tests. If you create a PR, the tests will automatically run on
GitHub actions on each push. We will only merge pull requests after a green GitHub actions run.To run tests locally for development you have multiple options:
1. Run tests against a specific Ruby version:
- Install and switch to the Ruby version
- Install development dependencies using `bundle install`
- Run tests using `bundle exec rspec`2. Run tests against all Ruby versions:
- Install all Ruby versions mentioned in `.github/workflows/test.yml`
- run `bin/matrix` (only supports `rbenv` for switching Ruby versions currently)Hints:
- At the time of writing this, we only have a single Gemfile. If that isn't the case any longer,
check the gemika README for more detailed development instructions.
- We recommend to have sufficiently new versions of bundler (> 2.3.0) and rubygems (> 3.3.0) installed for each Ruby version.
- The script `bin/matrix` will warn you, if that is not the case. For all other methods you need to ensure that yourself.## License
See [LICENSE.txt](https://github.com/makandra/memoized/blob/master/LICENSE.txt)
## Credits
- This gem is a fork of [Memoizer](https://github.com/wegowise/memoizer) by [Wegowise](https://www.wegowise.com/).
- Changes in this fork by [makandra](https://makandra.com).