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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -