Eva*_*n V 0 ruby ruby-on-rails
我正在研究无脂CRM的源代码.我试图了解其中一个app帮助程序中的这一特定代码行:
options[:selected] = (@account && @account.id) || 0
Run Code Online (Sandbox Code Playgroud)
它似乎是将选项哈希:selected值设置为实例变量的值@account或0(如果@account不存在).
什么是&& @account.id做什么?
它确保@account不是假的,如果不是,它会设置帐户的选项id.书面长手就等于这个:
options[:selected] = if @account && @account.id
@account.id
else
0
end
Run Code Online (Sandbox Code Playgroud)
要么
options[:selected] = (@account && @account.id) ? @account.id : 0
Run Code Online (Sandbox Code Playgroud)
我可能会使用它看起来像这样的andand宝石:
options[:selected] = @account.andand.id || 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |