ActiveSupport::Memoizes 指的是哪种 Ruby memoize 模式?
因此在 Rails 3.2 中,ActiveSupport::Memoizes 已被弃用。
消息内容如下:
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
还有其他我错过的东西吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是 Memoized 被弃用的提交(以及后续讨论):https://github.com/rails/rails/commit/36253916b0b788d6ded56669d37c96ed05c92c5c
作者提倡
@foo ||= ...
方法并指向此提交 作为迁移示例:https://github.com/rails/rails/commit/f2c0fb32c0dce7f8da0ce446e2d2f0cba5fd44b3。编辑:
请注意,我不一定将此更改解释为意味着
memoize
的所有实例都可以或应该替换为此模式。我认为这意味着 Rails 代码本身不再需要/不需要 Memoizable。正如评论所指出的,Memoizes 不仅仅是@foo ||= ...
的包装。如果您需要这些功能,请继续使用 Memoizes,您只需从 ActiveSupport 以外的其他地方获取它(我猜有人会分叉一个 gem 版本,如果他们还没有的话)。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).另一种选择是使用 Memoist gem:
它是从
ActiveSupport::Memoizes
直接提取的,可以用作插件 替代品。只需require 'memoist'
并更改为
Another option is to use the Memoist gem:
It is a direct extraction from
ActiveSupport::Memoizable
and can be used as a drop-in replacement. Justrequire 'memoist'
and changeto
只是对最佳答案的补充,要记住类方法,请使用以下模式:
Just an addition to the top answer, to memoize a class method use the following pattern:
基于提交 上面引用的avaynshtok,我要这样说:
…因为我想我会知道什么时候
Memoizes
从我的 RSpec 套件中从 ActiveSupport 中剥离出来,一开始就死掉了。Based upon the comments on the commit referenced above by avaynshtok, I’m going with this:
… because I figure I’ll know when
Memoizable
is ripped out of ActiveSupport from my RSpec suite dying right out of the starting gate.