我们什么时候使用“||=” Rails 中的运算符 ?其意义何在?
可能的重复:
运算符 ||= 在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让我们分解一下:
这就是说,如果
@_current_user
为nil
、false
,则将其设置为{SOMETHING}
,或未定义。否则将其设置为@_current_user
,或者换句话说,什么都不做。扩展形式:好的,现在到右侧。
您通常会看到
&&
仅包含布尔值,但在 Ruby 中您不必这样做。这里的技巧是,如果session[:current_user_id]
不为零,并且User.find(session[:current_user_id])
不为零,则表达式将计算为 < code>User.find(session[:current_user_id]) 否则为零。因此,将它们全部放在伪代码中:
Lets break it down:
This is saying, set
@_current_user
to{SOMETHING}
if it isnil
,false
, or undefined. Otherwise set it to@_current_user
, or in other words, do nothing. An expanded form:Ok, now onto the right side.
You usually see
&&
with boolean only values, however in Ruby you don't have to do that. The trick here is that ifsession[:current_user_id]
is not nil, andUser.find(session[:current_user_id])
is not nil, the expression will evaluate toUser.find(session[:current_user_id])
otherwise nil.So putting it all together in pseudo code:
这就是缓存能力。
当您从数据库加载当前用户时,这很有用,如果这是之前加载的,则语句将不会尝试评估方程的右侧部分,即 DRY,因此您可以将其视为缓存运算符。
参考:
http://railscasts.com/episodes/1-caching-with-instance-variables
This is caching abilities.
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
首先在 C 中流行的是二元运算符简写,例如:
其作用如下:
重新排列以实现更有效的短路是处理 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:
acts like:
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.
如果您有 C# 经验,我相信它与 C# 中的空合并 (??) 运算符类似(但更多的是 Ruby 技巧),
如果 x 不为 null,则将 x 分配给 y,否则为“默认”值使用-1。
同样,我认为 ||= 被称为 T 平方运算符。
或
来到你的声明
基本上,它会查看 @_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#
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.
or
Coming to your statement
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