Possible Duplicate: What does the operator
||=
最先在C语言中流行起来的二进制运算符速记,例如:。
就像:
为了更有效的短路而进行的重排是一种处理nil检查的优雅方式,因为如果可以的话,它完全避免了赋值。赋值可能有副作用。|= is called the T-square operator I believe.
@_current_user ||= session[:current_user_id] &&
User.find(session[:current_user_id])
orLook at the blog below for more info on the "T-square" operator:
@_current_user ||= {SOMETHING}
http:@_current_user
请看 {SOMETHING}
http:/www.rubyinside.comwhat-rubys-double-pipe-or-equals-really-does-5488.htmlnil
以获得更详细的解释。false
@_current_user
@_current_user || @_current_user = {SOMETHING}
如果你有C#的经验,我相信它类似于C#中的null-coalescing (?)操作符(但更多的是Ruby技巧)。
session[:current_user_id] &&
User.find(session[:current_user_id])
如果x不为空,则x被分配给y,否则使用 "默认 "值-1。&&
同样,/blogs.oracle.comprashantentrythe_ruby_t_square_operator也是如此。session[:current_user_id]
User.find(session[:current_user_id])
User.find(session[:current_user_id])