在Ruby中变量赋值双管道符号? [英] Double Pipe Symbols in Ruby Variable Assignment?

查看:278
本文介绍了在Ruby中变量赋值双管道符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  是什么|| =在Ruby中是什么意思?

原谅我,如果这是一个牛逼的问题,但即时阅读一本书上轨所在的作家在一个辅助方法使用该前pression:

Forgive me if this is a newby question but im reading a book on rails where the writer used this expression in a helper method:

@current_user ||= User.find_by_id(session[:user_id])

这是使用双管仍然是一个布尔OR语句?

Is this use of double pipes still a Boolean OR statement?

如果这样,它是如何工作的?

If so how does it work?

推荐答案

这是一个有条件的分配。从这里

It's a conditional assignment. From here:

 x = find_something() #=>nil
 x ||= "default"      #=>"default" : value of x will be replaced with "default", but only if x is nil or false
 x ||= "other"        #=>"default" : value of x is not replaced if it already is other than nil or false

这篇关于在Ruby中变量赋值双管道符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆