Using presenter rails - memoizable becomes deprecated in 3.1 - use || instead =?

Problem. To avoid creating multiple objects or multiple queries when possible.

I use Leaders with rails as a best practice.

I am following tips that say it would be useful to use "extend ActiveSupport.Memoizable" (and then memoize: method (s) to use them) over setting up elements with the style of @the_record = record ||= @record because of several problems - false or nil not to get the request to be called again, and also that memoizable uses the cache better (i.e. uses it!).

However, I see that memoizable is becoming obsolete on rails 3.1 Notes I github under the carrier and with the expression: "DEPARTMENT WARNING: ActiveSupport :: Memoizable is deprecated and will be removed in future releases, use the memoization Ruby template instead. (Called from the sequel at / Users / kain / .rvm / gems / ruby-1.9.3-preview1 / bundler / gems / carrierwave -c4459179b0f8 / Library / carrierwave / mount.rb: 284 "

Maybe it was allowed? Somebody knows?

Any suggestions for future best practices? Use syntax || =? How about the above issues?

enter image description here

+6
source share
1 answer

The ||= method ||= great for things that return values ​​that are evaluated as true, but this doesn't work very well for things that don't. memoize works around this, capturing these conditions and returning accordingly. You can do this if you want to place nil :

 def some_method return @some_method if (instance_variable_defined?(:"@some_method")) @some_method = begin ... end end 

It just checks if the variable is defined, and not if it is set, which is an important difference in your case.

I am not sure why you think this is obsolete [Note from Michael, it is deprecated in 3.2, see note below]. The documentation indicates that it is still relevant in 3.1. Sometimes implementations are marked as "deprecated" when they move from one module to another, but the tool remains available.

+7
source

Source: https://habr.com/ru/post/900289/


All Articles