method

memoize

Importance_5
Ruby on Rails latest stable (v2.3.4) - 6 notes - Class: ActiveSupport
  • v1.0.0
  • v1.1.0
  • v1.1.1
  • v1.1.2
  • v1.1.3
  • v1.1.4
  • v1.1.5
  • v1.1.6
  • v1.2.0
  • v1.2.1
  • v1.2.2
  • v1.2.3
  • v1.2.4
  • v1.2.5
  • v1.2.6
  • v2.0.0
  • v2.0.1
  • v2.0.2
  • v2.0.3
  • v2.1.0
  • 2.2.1 (0)
  • v2.3.2
  • v2.3.4
  • What's this?

Method deprecated

This method is deprecated on the latest stable version of Rails. The last existing version (v2.2.1) is shown here.

memoize(*symbols) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Show source
Register or log in to add new notes.
March 4, 2009
7 thanks

Differences between normal or-assign operator

Differences between this method and normal memoization with ||=:

  • memoize works with false/nil values
  • Potential arguments are memoized

Take the following example:

  def allowed?
    @allowed ||= begin
      # Big calculation
      puts "Worked"
      false
    end
  end

  allowed? # Outputs "Worked"
  allowed? # Outputs "Worked" again

Since @allowed is set to false (this is also applicable with nil), the ||= operator will move on the the next statement and will not be short-circuited.

When you use memoize you will not have this problem.

  def allowed?
    # Big calculation
    puts "Worked"
    false
  end
  memoize :allowed?

  allowed? # Outputs "Worked"
  allowed? # No output

Now, look at the case where we have parameters:

  def random(max=10)
    @random ||= rand(max)
  end

  random     # => 4
  random     # => 4 -- Yay!
  random(20) # => 4 -- Oops!

Better use memoize again!

  def random(max=10)
    rand(max)
  end
  memoize :random

  random     # => 6
  random     # => 6 -- Yay!
  random(20) # => 12 -- Double-Yay!
  random     # => 6 -- Head a'splode
January 20, 2009 - (>= v2.2.1)
4 thanks

Reloading memoized values

Memoize is used to cache the result of a method. It’s roughly equivalent of having:

  def memoized_method(*args)
    @result[args] ||= (
      # do calculation here
    )
  end

However, the result is cached so that it’s not calculated for every request.

To recalculate cached value use either

  obj.memoized_method(:reload)

or

  obj.memoized_method(true)
January 16, 2009 - (>= v2.2.1)
4 thanks

Remember to mixin the ActiveSupport::Memoizable module

To use memoize in your model you need to extend the model class with the module, like this:

  class Person < ActiveRecord::Base
    # Mixin the module
    extend ActiveSupport::Memoizable

    def expensive_method
      # do something that is worth remembering
    end
    memoize :expensive_method
  end

If you use memoizable in most of your models you could consider mixing the module into all ActiveRecord models by doing this in an initializer:

  ActiveRecord::Base.extend(ActiveSupport::Memoizable)
June 1, 2009 - (v2.2.1 - v2.3.2)
1 thank

Further To: Memoize will not cache singleton methods

er…it will:

Code example

  class PersonType < ActiveRecord::Base
    class << self
      # Add the mixin here:
      extend ActiveSupport::Memoizable
      def mister
        find_by_name('Mister')
      end
      memoize :mister
    end
  end
October 15, 2009
1 thank

Current version

See ActiveSupport::Memoizable for un-deprecated version.

March 9, 2009
0 thanks

Memoize will not cache singleton methods

The following does not work:

  class PersonType < ActiveRecord::Base
    extend ActiveSupport::Memoizable
    class << self
      def mister
        find_by_name('Mister')
      end
      memoize :mister
    end

I guess one could extend the superclass, Class, with Memoizable support, but that seems Evil.