Is there a Rails/Ruby idiom for checking if an enumerable is both present and has non-nil values?
I get errors if I ever try to do nil.any? so I always have to do if foo && foo.any?.
You can use the try method provided by ActiveSupport:
obj.try(:any?)
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.
I believe you can now use the safe navigation operator:
if foo&.any?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With