Memoize

Directive for making your functions faster by trading space for time. When you "memoize" a method/function its results are cached so that later calls with the same arguments returns results in the cache instead of recalculating them.

  class T
    def initialize(a)
      @a = a
    end
    def a
      "#{@a ^ 3 + 4}"
    end
    memoize :a
  end

  t = T.new(1)
  t.a.__id__ == t.a.__id__  #=> true

Authors

  • trans
  • rtb

Copying

Copyright (c) 2005 Robert Feldt

Ruby License

This module is free software. You may use, modify, and/or redistribute this software under the same terms as Ruby.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Methods
memoize
Public Instance methods
memoize(*args)

This allows memoize to work in main and instance scope too.

# File lib/more/facets/memoize.rb, line 82
def memoize(*args)
  Object.memoize(*args)
end