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

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -