NumericMemoist
The memoize library allows you to cache methods for Numerics (Integer/Float) in Ruby >= 2.0.0.
Installation
Add this line to your application's Gemfile:
gem 'numeric_memoist'
And then execute:
$ bundle
Or install it yourself as:
$ gem install numeric_memoist
Usage
Open class Integer
/Float
, extend NumericMemoist
like the following:
require 'numeric_memoist'
class Integer
extend NumericMemoist
def sq
self ** 2
end
memoize :sq
end
Then Integer#sq will only be calcurated once. (These work on Ruby >= 2.0.0, and also <= 1.9.x.)
Comparison to others
On Ruby <= 1.9.x, you may code as follows:
class Integer
def sq
@sq ||= self ** 2
# This raises RuntimeError on Ruby >= 2.0.x
end
end
This doesn't work on Ruby >= 2.0.0, since Integer is frozen.
You may use another similar gem like Memoist:
require 'memoist'
class Integer
extend Memoist
def sq
self ** 2
end
memoize :sq
# No Error raised, but momeize doesn't work on Ruby >= 2.0.x
end
This doesn't work on Ruby >= 2.0.0 for the same reason.
For newer Rubies
NumericMemoist works well with Ruby 2.4.0 (tested with 2.4.0-preview2).
An old example (bundled with NumericMemoist v0.0.1) does not work with Ruby >= 2.4.0 (for Integer
's specification change). Updated example(s) in NumericMemoist >= v0.0.2 .
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Acknowledgements
Inspired by Memoist.
License
Under the MIT License.