I am using the following code to check if a variable is not nil and not zero
if(discount != nil && discount != 0)
...
end
Is the
if (discount||0) != 0
#...
end
unless [nil, 0].include?(discount) # ... end
if discount.nil? || discount == 0
[do something]
end
if discount and discount != 0
..
end
update, it will false
for discount = false
I prefer using a more cleaner approach :
val.to_i.zero?
val.to_i
will return a 0
if val is a nil
,
after that, all we need to do is check whether the final value is a zero.
When dealing with a database record, I like to initialize all empty values with 0, using the migration helper:
add_column :products, :price, :integer, default: 0