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

I understand what I can do

unless [1].empty? 

But I wonder if there is a way?

+46
ruby
Aug 2 '12 at 16:14
source share
4 answers

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).

+68
Aug 02 2018-12-12T00:
source share

Can you use [1].any? which is actually defined in Enumerable

Note that this will not work if your array contains only null or false values ​​(thanks for the comment by @InternetSeriousBusiness).

+14
Aug 02 '12 at 16:15
source share
 [nil].any? => false [nil].any? {|something| true} => true [].any? {|something| true} => false [false, false].any? {|something| true} => true [nil, 'g'].any? {|something| true} => true 
+6
Dec 31 '15 at 4:38
source share

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? 
-one
May 23 '13 at 16:14
source share



All Articles