What is the opposite method of Array.empty? or [].empty? in ruby

You may use [1].any?, which is actually defined in Enumerable

Note that this will not work in case your array hold only nil or false values.


[nil].any?
=> false
[nil].any? {|something| true}
=> true
[].any? {|something| true}
=> false
[false, false].any? {|something| true}
=> true
[nil, 'g'].any? {|something| true}
=> true

As well as #any? as davidrac mentioned, with ActiveSupport there's #present? which acts more like a truth test in other languages. For nil, false, '', {}, [] and so on it returns false; for everything else true (including 0, interestingly).

Tags:

Ruby