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?.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
trymethod provided by ActiveSupport:This will evaluate to
nilifobj.nil?or tofalseifobjis an empty collection, so in both cases it will evaluate to a falsy value in a boolean context.