在 Ruby 中,我应该使用 ||= 还是如果已定义? 为了记忆?

发布于 2024-07-23 14:46:57 字数 428 浏览 13 评论 0原文

我应该使用 if Defined?

 return @current_user_session if defined?(@current_user_session)
 @current_user_session = UserSession.find

还是 ||=

@current_user_session ||= UserSession.find

我注意到 if Defined? 方法最近被越来越多地使用。 其中一种相对于另一种有什么优势吗? 就我个人而言,为了可读性,我更喜欢 ||= 。 我还认为 Rails 可能有一个 memoize 宏,它透明地提供了这种行为。 是这样吗?

Should I use if defined?

 return @current_user_session if defined?(@current_user_session)
 @current_user_session = UserSession.find

Or ||=

@current_user_session ||= UserSession.find

I noticed the if defined? method being used more and more recently. Is there any advantage to one over the other? Personally, I prefer ||= for readability. I also think Rails might have a memoize macro which provides this behavior transparently. Is this the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦断已成空 2024-07-30 14:46:57

此外,更好的 ||= 会生成有关未初始化实例变量的警告(至少在 1.8.6 和 1.8.7 上),而更详细的 defineed? 版本则不会不是。

另一方面,这可能会满足您的要求:

def initialize
  @foo = nil
end

def foo
  @foo ||= some_long_calculation_for_a_foo
end

但这几乎肯定不会:

def initialize
  @foo = nil
end

def foo
  return @foo if defined?(@foo)
  @foo = some_long_calculation_for_a_foo
end

因为 @foo总是在此时定义。

Additionally, the nicer ||= produces a warning (on 1.8.6 and 1.8.7, at least) about uninitialized instance variables, while the more verbose defined? version does not.

On the other hand, this probably does what you want:

def initialize
  @foo = nil
end

def foo
  @foo ||= some_long_calculation_for_a_foo
end

But this almost certainly does not:

def initialize
  @foo = nil
end

def foo
  return @foo if defined?(@foo)
  @foo = some_long_calculation_for_a_foo
end

since @foo will always be defined at that point.

雨夜星沙 2024-07-30 14:46:57

Rails 确实有记忆功能,请查看下面的截屏视频以获取精彩的介绍:

http://railscasts.com/episodes /137-记忆化

class Product < ActiveRecord::Base
  extend ActiveSupport::Memoizable

  belongs_to :category

  def filesize(num = 1)
    # some expensive operation
    sleep 2
    12345789 * num
  end

  memoize :filesize
end

Rails does have memoization, check out the screencast below for a great introduction:

http://railscasts.com/episodes/137-memoization

class Product < ActiveRecord::Base
  extend ActiveSupport::Memoizable

  belongs_to :category

  def filesize(num = 1)
    # some expensive operation
    sleep 2
    12345789 * num
  end

  memoize :filesize
end
看轻我的陪伴 2024-07-30 14:46:57

请注意:如果 x 返回 false,则 x ||= y 会指定 x = y。 这可能意味着 x 未定义、nil 或 false。

很多时候变量会被定义并且为 false,尽管可能不是在 @current_user_session 实例变量的上下文中。

如果您希望简洁,请尝试条件构造:

defined?(@current_user_session) ?
    @current_user_session : @current_user_session = UserSession.find

或 just:

defined?(@current_user_session) || @current_user_session = UserSession.find

如果您只需要初始化变量。

Be careful: x ||= y assigns x = y if x returns false. That may mean that x is undefined, nil, or false.

There are many times variables will be defined and false, though perhaps not in the context of the @current_user_session instance variable.

If you desire conciseness, try the conditional construct:

defined?(@current_user_session) ?
    @current_user_session : @current_user_session = UserSession.find

or just:

defined?(@current_user_session) || @current_user_session = UserSession.find

if you just need to initialize the variable.

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