ruby on rails - What is a better way to check for a nil object before calling a method on it? -
I have a call to this method that I want to use ...
financial_document .assets length
but financial_document.assets
can be zero
.
I could use ...
financial_document.assets.nil? ? '0': financial_document.assets.length
Is there a repetitive method to do this?
Personally, I use or
operator / Keywords:
(financial_document.assets or []
By any means, say .length
on an array Goes to 0
if zero
.
Comments
Post a Comment