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 -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -