I understand what I can do
unless [1].empty?
But I wonder if there is a way?
Like #any? as mentioned in davidrac, ActiveSupport #present? which is more like a truth test in other languages. For nil , false , '' , {} , [] , etc. It returns false; for everything else true (including 0, interesting).
#any?
nil
false
''
{}
[]
Can you use [1].any? which is actually defined in Enumerable
[1].any?
Note that this will not work if your array contains only null or false values (thanks for the comment by @InternetSeriousBusiness).
[nil].any? => false [nil].any? {|something| true} => true [].any? {|something| true} => false [false, false].any? {|something| true} => true [nil, 'g'].any? {|something| true} => true
Check the elements in the array:.empty?.present?
if a = {}a.any? .nil?will lead to false.
To check if a field has a value other than zero:
.present? .nil? .any?