Rails if statement is getting long... when it should be simple -
is there way write code without writing word foo twice?
foo? foo : 0
in case, foo
vehicle.maintenance.starting_odometer
can imagine if have check presence on objects these code gets pretty lengthy , ugly.
sure, can use ||
:
foo || 0
the disjunctive operator (known or
or ||
) first looks @ left-hand element. if truthy (not nil
or false
) returns that. otherwise, evaluates , returns right-hand element. ternary operator works same way, except allows specify different value in place of original 'left-hand' element.
Comments
Post a Comment