我们什么时候使用“||=” Rails 中的运算符 ?其意义何在?

发布于 2024-11-01 16:48:25 字数 436 浏览 3 评论 0原文

可能的重复:
运算符 ||= 在 ruby​​ 中代表什么?

我对 Rails 中 ||= 运算符的使用感到困惑。我在网上找不到任何有用的东西。有人可以指导我吗?

如果您知道任何网络链接,请告诉我。

我想知道以下声明的含义:

@_current_user ||= session[:current_user_id] &&
      User.find(session[:current_user_id])

Possible Duplicate:
What does the operator ||= stands for in ruby?

I am confused with the usage of ||= operator in Rails. I couldn't locate anything useful on the web. Can anyone please guide me?

Do let me know if there are any weblinks that you are aware of.

I would like what the following statement means:

@_current_user ||= session[:current_user_id] &&
      User.find(session[:current_user_id])

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

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

发布评论

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

评论(4

梦一生花开无言 2024-11-08 16:48:25

让我们分解一下:

@_current_user ||= {SOMETHING}

这就是说,如果 @_current_usernilfalse,则将其设置为 {SOMETHING} ,或未定义。否则将其设置为@_current_user,或者换句话说,什么都不做。扩展形式:

@_current_user || @_current_user = {SOMETHING}

好的,现在到右侧。

session[:current_user_id] &&
      User.find(session[:current_user_id])

您通常会看到 && 仅包含布尔值,但在 Ruby 中您不必这样做。这里的技巧是,如果 session[:current_user_id] 不为零,并且 User.find(session[:current_user_id]) 不为零,则表达式将计算为 < code>User.find(session[:current_user_id]) 否则为零。

因此,将它们全部放在伪代码中:

if defined? @_current_user && @_current_user
  @_current_user = @_current_user
else
  if session[:current_user_id] && User.find(session[:current_user_id])
    @_current_user = User.find(session[:current_user_id])
  else
    @_current_user = nil
  end
end

Lets break it down:

@_current_user ||= {SOMETHING}

This is saying, set @_current_user to {SOMETHING} if it is nil, false, or undefined. Otherwise set it to @_current_user, or in other words, do nothing. An expanded form:

@_current_user || @_current_user = {SOMETHING}

Ok, now onto the right side.

session[:current_user_id] &&
      User.find(session[:current_user_id])

You usually see && with boolean only values, however in Ruby you don't have to do that. The trick here is that if session[:current_user_id] is not nil, and User.find(session[:current_user_id]) is not nil, the expression will evaluate to User.find(session[:current_user_id]) otherwise nil.

So putting it all together in pseudo code:

if defined? @_current_user && @_current_user
  @_current_user = @_current_user
else
  if session[:current_user_id] && User.find(session[:current_user_id])
    @_current_user = User.find(session[:current_user_id])
  else
    @_current_user = nil
  end
end
私藏温柔 2024-11-08 16:48:25

这就是缓存能力。

a ||= 1  # a assign to 1
a ||= 50 # a is already assigned, a will not be assigned again

puts a
#=> 1

当您从数据库加载当前用户时,这很有用,如果这是之前加载的,则语句将不会尝试评估方程的右侧部分,即 DRY,因此您可以将其视为缓存运算符。

参考:
http://railscasts.com/episodes/1-caching-with-instance-variables

This is caching abilities.

a ||= 1  # a assign to 1
a ||= 50 # a is already assigned, a will not be assigned again

puts a
#=> 1

this is useful when u load current user from DB, if this is loaded prior, statement will not try to evaluate right part of equation, which DRY, therefore u can consider it as caching operator.

REF:
http://railscasts.com/episodes/1-caching-with-instance-variables

弃爱 2024-11-08 16:48:25

首先在 C 中流行的是二元运算符简写,例如:

a += b      # and...
a ||= b

其作用如下:

a = a + b   # and ... note the short circuit difference ... 
a || a = b

重新排列以实现更有效的短路是处理 nil 检查的一种优雅方式,因为它可以完全避免赋值(如果可以的话)。该作业可能会产生副作用。这只是 Ruby 中经过深思熟虑的设计的另一个例子。

请参阅 http://www.rubyinside .com/what-rubys-double-pipe-or-equals-really-does-5488.html 以获得更冗长的解释。

First made popular in C, the binary operator shorthand, for example:

a += b      # and...
a ||= b

acts like:

a = a + b   # and ... note the short circuit difference ... 
a || a = b

The rearrangement for a more effective short circuit is a graceful way of dealing with a check for nil as it avoids the assignment altogether if it can. The assignment may have side effects. Just another example of seriously thoughtful design in Ruby.

See http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html for a more wordy explanation.

可遇━不可求 2024-11-08 16:48:25

如果您有 C# 经验,我相信它与 C# 中的空合并 (??) 运算符类似(但更多的是 Ruby 技巧),

int? y = x ?? -1

如果 x 不为 null,则将 x 分配给 y,否则为“默认”值使用-1。

同样,我认为 ||= 被称为 T 平方运算符。

a || = b

a || a = b

来到你的声明

@_current_user ||= session[:current_user_id] &&
    User.find(session[:current_user_id])

基本上,它会查看 @_current_user 是否为零。如果它具有某些值,请保留它(当前用户)。否则,使用用户 ID 从会话中获取当前用户。它首先查看 id 是否在会话中,然后从 User 获取。

有关“T 方”运算符的更多信息,请参阅下面的博客:

http://blogs。 oracle.com/prashant/entry/the_ruby_t_square_operator

If you have C# experience, I believe it is similar ( but more of a Ruby-trick ) to the null-coalescing (??) operator in C#

int? y = x ?? -1

x is assigned to y if x is not null, otherwise the "default" value of -1 is used.

Similarly, ||= is called the T-square operator I believe.

a || = b

or

a || a = b

Coming to your statement

@_current_user ||= session[:current_user_id] &&
    User.find(session[:current_user_id])

Basically, it sees if @_current_user is nil or not. If it has some value, leave it alone ( the current user. ) Else, get the current user from the session using the user id. It first sees if the id is in the session, and then gets from the User.

Look at the blog below for more info on the "T-square" operator:

http://blogs.oracle.com/prashant/entry/the_ruby_t_square_operator

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