method

memoize

Importance_3
v2.3.8 - Show latest stable - 4 notes - Class: ActiveSupport::Memoizable
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.
December 12, 2011
2 thanks

Need to extend class when using this

I had to dig around to find this out - if you want to use memoize somewhere, like an ActiveRecord model, you need to add

extend ActiveSupport::Memoizable

to the class. This doesn’t seem to be explained anywhere (the only docs are 3 year old blog posts anyway).

April 27, 2012 - (>= v3.2.1)
1 thank

Example from Code School

module Tweets

 class ShowPresenter
   extend ActiveSupport::Memoizable

   def initialize(tweet)
     @tweet = tweet
   end

   def username
     @tweet.user.username
   end

   def status
     @tweet.status
   end

   def favorites_count
     @tweet.favorites.count
   end

   memoize :username, :status, :favorites_count

 end
end

From http://railsbest.com/

July 9, 2012
0 thanks

this has been deprecated; replace with Memoist

In Rails 3.2, memoize has been deprecated. In edge Rails, it has been removed.

The commit when it was deprecated: http://github.com/rails/rails/commit/36253916b0b788d6ded56669d37c96ed05c92c5c

A Stack Overflow question about this change: http://stackoverflow.com/q/9132197/578288

I personally disagree with the removal of memoize, and don’t recommend using the `||=` pattern Rails now suggests. The exception is if your entire program only memoizes something once or twice, so it’s not worth including a gem for.

The easiest way to keep using memoize is to use the Memoist gem (http://github.com/matthewrudy/memoist , http://rubygems.org/gems/memoist), which is a simple extraction of ActiveSupport::Memoizable into its own gem.

November 10, 2015
0 thanks

instead of memoize

See this for deprecated github.com/rails/rails/commit/36253916b0b788d6ded56669d37c96ed05c92c5c

use

def something
  return @_var if defined? @_var
  # more code
end