Memoize method return values
See CHANGELOG.md for details.
See CONTRIBUTING.md for details.
Memoization is an optimization that saves the return value of a method so it doesn't need to be re-computed every time that method is called. For example, perhaps you've written a method like this:
class Planet
# This is the equation for the area of a sphere. If it's true for a
# particular instance of a planet, then that planet is spherical.
def spherical?
4 * Math::PI * radius ** 2 == area
end
endThis code will re-compute whether a particular planet is spherical every time the method is called. If the method is called more than once, it may be more efficient to save the computed value in an instance variable, like so:
class Planet
def spherical?
@spherical ||= 4 * Math::PI * radius ** 2 == area
end
endOne problem with this approach is that, if the return value is false, the
value will still be computed each time the method is called. It also becomes
unweildy for methods that grow to be longer than one line.
These problems can be solved by mixing-in the Memoizable module and memoizing
the method.
require 'memoizable'
class Planet
include Memoizable
def spherical?
4 * Math::PI * radius ** 2 == area
end
memoize :spherical?
endThe example above assumes that the radius and area of a planet will not change over time. This seems like a reasonable assumption but such an assumption is not safe in every domain. If it was possible for one of the attributes to change between method calls, memoizing that value could produce the wrong result. Please keep this in mind when considering which methods to memoize.
This library aims to support and is tested against the following Ruby versions:
- Ruby 3.2
- Ruby 3.3
- Ruby 3.4
- Ruby 4.0
If something doesn't work on one of these versions, it's a bug.
Copyright © 2013-2026 Dan Kubb, Erik Berlin. See LICENSE for details.