jdo*_*dog 3 ruby ruby-on-rails
我有一个boolean var的对象.
field :processing, :type => Boolean
Run Code Online (Sandbox Code Playgroud)
在我面前的开发人员写了一些代码说这个.
:processing => nil
Run Code Online (Sandbox Code Playgroud)
(出于某种原因,他将其设置为nil而不是false.)
然后他做了这个if语句
return if self.processing
dosomethingelse....
Run Code Online (Sandbox Code Playgroud)
如果我编写代码来执行此操作
:processing => false
Run Code Online (Sandbox Code Playgroud)
下次运行此代码时会发生什么?dosomethingelse运行吗?
return if self.processing
dosomethingelse....
Run Code Online (Sandbox Code Playgroud)
更新===========
对于下面的许多问题,我们将在此回答.
我加了这个
field :processing, :type => Boolean, :default => false
Run Code Online (Sandbox Code Playgroud)
它打破了应用程序.当我改变到上面的dosomethingelse永远不会运行?
return if self.processing回报.有什么建议?
更新2 =======================================
以下是对我的代码(编辑)中的处理的每个引用.如果重要的话,我也在使用MongoDB.
.where(:processing => nil).gt(:retries => 0).asc(:send_time).all.entries
if self.processing
end
return if self.processing
self.update_attributes(:processing => true)
dosomethingelse....
.where(:sent_time => nil).where(:processing => nil).gt(:retries => 0).asc(:send_time).all.entries
:processing => nil
Run Code Online (Sandbox Code Playgroud)
Ruby使用truthy和falsey.
false和nil是falsey,其他一切都是truthy.
if true
puts "true is truthy, duh!"
else
puts "true is falsey, wtf!"
end
Run Code Online (Sandbox Code Playgroud)
输出是 "true is truthy, duh!"
if nil
puts "nil is truthy"
else
puts "nil is falsey"
end
Run Code Online (Sandbox Code Playgroud)
输出是 "nil is falsey"
if 0
puts "0 is truthy"
else
puts "0 is falsey"
end
Run Code Online (Sandbox Code Playgroud)
输出是 "0 is truthy"
看到这个解释是真是假
您可以使用双重否定将对象“转换”为布尔值:
!!nil # false
!!false # false
!!true # true
Run Code Online (Sandbox Code Playgroud)
一般来说,仅nil和false给出false结果。因此,在if陈述中nil和false是可以互换的。
| 归档时间: |
|
| 查看次数: |
11820 次 |
| 最近记录: |