Memoizable

Memoize method return values

Gem Version Build Status Dependency Status Code Climate Coverage Status

Contributing

See CONTRIBUTING.md for details.

Rationale

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
end

This 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
end

One 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?
end

Warning

The 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.

Copyright © 2013 Dan Kubb, Erik Michaels-Ober. See LICENSE for details.