Which Ruby memoize template supports ActiveSupport :: Memoizable?

So, in Rails 3.2 ActiveSupport :: Memoizable is deprecated.

The message says:

DEPRECATION WARNING: ActiveSupport::Memoizable is deprecated and will be removed in future releases,simply use Ruby memoization pattern instead. 

It refers to the "Ruby memoization pattern" (singular), as if we all knew one pattern and referred to ...

I assume they mean something like:

 def my_method @my_method ||= # ... go get the value end 

or

 def my_method return @my_method if defined?(@my_method) @my_method = # ... go get the value end 

Is there anything else I missed?

+54
ruby ruby-on-rails-3 memoization activesupport
Feb 03 '12 at 16:45
source share
4 answers

Here is the commit (and subsequent discussion) where Memoizable is deprecated: https://github.com/rails/rails/commit/36253916b0b788d6ded56669d37c96ed05c92c5c

The author advocates the @foo ||= ... approach, and points out this commit as an example of migration: https://github.com/rails/rails/commit/f2c0fb32c0dce7f8da0ce446e2d2f0cba5fd44b3 .

Edit: Note that I do not necessarily interpret this change as meaning that all instances of memoize can or should be replaced with w / this pattern. I read this as meaning that Memoizable is no longer needed / not needed in Rails code. According to the comments, Memoizable is much more than just a wrapper around @foo ||= ... If you need these features, continue to use Memoizable, you just need to get it from somewhere other than ActiveSupport (I assume someone will fork the gem version if they haven't already).

+37
Feb 06 '12 at 6:16
source share
— -

Another option is to use Memoist memo:

This is a direct extract from ActiveSupport::Memoizable and can be used as a replacement. Just require 'memoist' and change

 extend ActiveSupport::Memoizable 

to

 extend Memoist 
+32
Apr 18 '12 at 8:13
source share

Just adding to the top answer so that the memoize class method uses the following pattern:

 class Foo class << self def bar @bar ||= begin # ... end end end end 
+3
Dec 03 '13 at 12:55
source share

Based on the comments the commit referenced above is avaynshtok , Im going with this:

 ActiveSupport::Deprecation.silence { extend ActiveSupport::Memoizable } 

... because I believe that I know when Memoizable breaks out of ActiveSupport from my RSpec package, dying right from the start server.

0
Jun 08 2018-12-12T00:
source share



All Articles