chug2k chug2k - 4 years ago 135
Ruby Question

Rails idiom for .present? and .any?

Is there a Rails/Ruby idiom for checking if an enumerable is both present and has non-


I get errors if I ever try to do
so I always have to do
if foo && foo.any?

Answer Source

You can use the try method provided by ActiveSupport:


This will evaluate to nil if obj.nil? or to false if obj is an empty collection, so in both cases it will evaluate to a falsy value in a boolean context.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download