Skip to content

flori/mize

Repository files navigation

Mize

Description

Library to memoize methods or functions in Ruby.

Installation

You can use rubygems to fetch the gem and install it for you:

# gem install mize

You can also put this line into your Gemfile

gem 'mize'

and bundle.

Usage

Memoizes methods, that is the values depend on the receiver, like this:

class A
  @@c = 0

  memoize method:
  def foo(x)
    "foo #{x} #{@@c += 1}"
  end
end

a1 = A.new
a1.foo(23) # => "foo 23 1"
a1.foo(23) # => "foo 23 1"
a2 = A.new
a2.foo(23) # => "foo 23 2"
a2.foo(23) # => "foo 23 2"
a2.mize_cache_clear
a2.foo(23) # => "foo 23 3"
a1.foo(23) # => "foo 23 1"

Memoizes functions, that is the values do not depend on the receiver, like this:

class B
  @@c = 0

  memoize function:
  def foo(x)
    "foo #{x} #{@@c += 1}"
  end
end

b1 = B.new
b1.foo(23) # => "foo 23 1"
b1.foo(23) # => "foo 23 1"
b2 = B.new
b2.foo(23) # => "foo 23 1"
b2.foo(23) # => "foo 23 1"
B.mize_cache_clear
b2.foo(23) # => "foo 23 2"
b1.foo(23) # => "foo 23 2"

Download

The homepage of this library is located at

Author

Florian Frank

License

This software is licensed under MIT license.

About

Memoize methods/functions in Ruby

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages