Project

mem

0.02
Low commit activity in last 3 years
A long-lived project that still receives updates
Memoize method calls.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Mem

CI

Memoize method calls.

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add mem

If bundler is not being used to manage dependencies, install the gem by executing:

gem install mem

Usage

class Foo
  include Mem

  def initialize
    @count = 0
  end

  def bar
    baz
  end

  # `memoize` defines bar_with_memoize & bar_without_memoize,
  # and the result of the 1st method call is stored into @memoized_table.
  memoize :bar

  private

  def baz
    @count += 1
  end
end

foo = Foo.new
foo.bar #=> 1
foo.bar #=> 1
foo.bar #=> 1
foo.has_memoized?(:bar) #=> true
foo.memoized(:bar) #=> 1
foo.memoized_table #=> { bar: 1 }

core ext

You can require "mem/core_ext" to skip include Mem, while this extends Object class.

require "mem/core_ext"

class A
  def x
    puts 1
  end
  memoize :x
end

a = A.new
a.x #=> 1
a.x #=> nothing logged out
a.x #=> nothing logged out