I have an object with a boolean var.
field :processing, :type => Boolean
The dev before me wrote some code that says this.
:processing => nil
(He is, for some reason, setting it to nil instead of false.)
He then does this if statement
return if self.processing
dosomethingelse....
If I write code that does this
:processing => false
what happens the next time this code runs? Does dosomethingelse run?
return if self.processing
dosomethingelse....
UPDATE ===========
To many questions below so will answer here.
I added this
field :processing, :type => Boolean, :default => false
and it broke the app. When I changed to the above dosomethingelse never gets run?return if self.processing
returns. Any suggestions?
UPDATE 2 =======================================
Here is every reference to processing in my code (redacted). Also I am using MongoDB if that matters.
.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
In Ruby, nil is a special value that denotes the absence of any value. Nil is an object of NilClass. nil is Ruby's way of referring to nothing or void.
in a boolean context (if, &&, ||, etc.). Ruby has to decide whether these values count as true or false. If the value isn't literally "true" but evaluates as true, we call it "truthy." Likewise, if the value isn't literally "false" but evaluates as false, we call it "falsey."
nil is certainly not a boolean, it is it's own special value that is used to represent null values. For example instance variables (e.g. @foo ) will by default evaluate to nil if they have not previously been set.
Ruby uses truthy
and falsey
.
false
and nil
are falsey
, everything else is truthy
.
if true
puts "true is truthy, duh!"
else
puts "true is falsey, wtf!"
end
Output is "true is truthy, duh!"
if nil
puts "nil is truthy"
else
puts "nil is falsey"
end
Output is "nil is falsey"
if 0
puts "0 is truthy"
else
puts "0 is falsey"
end
Output is "0 is truthy"
See this explanation True and False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With