Is it bad to check if an array is not empty by using any? method?
any?
a = [1,2,3] a.any? => true a.clear a.any? => false
Or is it better to use unless a.empty? ?
unless a.empty?
any? isn't the same as not empty? in some cases.
not empty?
>> [nil, 1].any? => true >> [nil, nil].any? => false
From the documentation:
If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is any? will return true if at least one of the collection members is not false or nil).
1.4m articles
1.4m replys
5 comments
57.0k users