Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Ruby memoize pattern does ActiveSupport::Memoizable refer to?

So in Rails 3.2, ActiveSupport::Memoizable has been deprecated.

The message reads:

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

It refers to "Ruby memoization pattern" (singular) as if there's one pattern we should all know and refer to...

I presume 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 something else I've missed?

like image 644
bodacious Avatar asked Feb 03 '12 16:02

bodacious


People also ask

How does memoization work in Ruby?

Put simply, memoization is saving a method's return value so it does not have to be recomputed each time. As with all caching, you are effectively trading memory for time (i.e. you give up the memory required to store the value, but you save the time required to process the method).

What is memoization Rails?

Memoization is a process that can be used to speed up rails methods. It caches the results of methods that do time-consuming work, work that only needs to be done once.


1 Answers

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

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

Edit: Note that I don't necessarily interpret this change as meaning that all instances of memoize can or should be replaced w/ this pattern. I read it as meaning that Memoizable is no longer needed/wanted in the Rails code itself. As the comments point out, Memoizable is much more than just a wrapper around @foo ||= .... If you need those features, go ahead and use Memoizable, you'll just have to get it from somewhere other than ActiveSupport (I'm guessing someone will fork a gem version, if they haven't already).

like image 117
avaynshtok Avatar answered Sep 20 '22 11:09

avaynshtok