memoize
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (0)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
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