Library to memoize methods or functions in Ruby.
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.
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"
The homepage of this library is located at
This software is licensed under MIT license.