Giter Site home page Giter Site logo

memoizable's Introduction

Memoizable

Gem Version Build Status Dependency Status Code Climate Coverage Status

Memoize method return values

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.

Supported Ruby Versions

This library aims to support and is tested against the following Ruby implementations:

If something doesn't work on one of these versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby versions or implementations, however support will only be provided for the implementations listed above.

If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.

Copyright

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

memoizable's People

Contributors

dkubb avatar sferik avatar trliner avatar ys avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.