ActiveSupport::Memoizes 指的是哪种 Ruby memoize 模式?

发布于 2025-01-01 17:49:02 字数 553 浏览 2 评论 0原文

因此在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怎会甘心 2025-01-08 17:49:02

以下是 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).

一梦浮鱼 2025-01-08 17:49:02

另一种选择是使用 Memoist gem:

它是从 ActiveSupport::Memoizes 直接提取的,可以用作插件 替代品。只需 require 'memoist' 并更改

extend ActiveSupport::Memoizable

extend 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. Just require 'memoist' and change

extend ActiveSupport::Memoizable

to

extend Memoist
暖风昔人 2025-01-08 17:49:02

只是对最佳答案的补充,要记住类方法,请使用以下模式:

class Foo
  class << self
    def bar
      @bar ||= begin
        # ...
      end
    end
  end
end

Just an addition to the top answer, to memoize a class method use the following pattern:

class Foo
  class << self
    def bar
      @bar ||= begin
        # ...
      end
    end
  end
end
差↓一点笑了 2025-01-08 17:49:02

基于提交 上面引用的avaynshtok,我要这样说:

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

…因为我想我会知道什么时候 Memoizes 从我的 RSpec 套件中从 ActiveSupport 中剥离出来,一开始就死掉了。

Based upon the comments on the commit referenced above by avaynshtok, I’m going with this:

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

… because I figure I’ll know when Memoizable is ripped out of ActiveSupport from my RSpec suite dying right out of the starting gate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文