What is a better way to check for a nil object before calling a method on it?
ruby, ruby-on-rails
Solution
Personally, I would use the `or` operator/keyword:
(financial_document.assets or []).length
Either way, `.length` is called on an array, giving you `0` if `nil`.
Problem
I have this method call I have to use... ``` financial_document.assets.length ``` But `financial_document.assets` could be `nil`. I could use... ``` financial_document.assets.nil? ? '0' : financial_document.assets.length ``` Is there a less repetitive way to do that?