memoize


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).

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

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.

instead of memoize
See this for deprecated github.com/rails/rails/commit/36253916b0b788d6ded56669d37c96ed05c92c5c
use
def something return @_var if defined? @_var # more code end