ActiveSupport :: Memoizable指的是哪种Ruby记忆模式? [英] Which Ruby memoize pattern does ActiveSupport::Memoizable refer to?

查看:98
本文介绍了ActiveSupport :: Memoizable指的是哪种Ruby记忆模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在Rails 3.2中,不建议使用ActiveSupport :: Memoizable.

该消息显示为:

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

它指的是"Ruby记忆模式"(单数形式),好像有一种我们都应该知道并参考的模式...

我认为它们的意思是:

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

def my_method
  return @my_method if defined?(@my_method)

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

还有其他我想念的东西吗?

解决方案

以下是不推荐使用Memoizable的提交(和后续讨论):指向此提交的点为例迁移: https://github.com/rails/rails/commit/f2c0fb32c0dce7f8da0ce446e2d2f0cb5> 请注意,我不必将此更改解释为可以或应该使用此模式替换memoize的所有实例.我将其理解为意味着在Rails代码本身中不再需要/不需要Memoizable.正如评论所指出的那样,Memoizable不仅仅是@foo ||= ...的包装.如果需要这些功能,请继续使用Memoizable,您只需从ActiveSupport以外的其他地方获得它(我猜有人会叉一个gem版本,如果他们还没有的话).

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?

解决方案

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

这篇关于ActiveSupport :: Memoizable指的是哪种Ruby记忆模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆