Ruby non_empty? method

I want to use the expression:

!([1,2,3] & [43,5]).empty? => false !([1,2,3] & [3,5]).empty? => true 

to check if two arrays contain at least one common value. And I wonder if there is a better way to do this? Maybe something like:

  ([1,2,3] & [3,5]).non_empty? 

How to write a non_empty? method non_empty? ?

+1
ruby refactoring
May 20 '10 at 15:18
source share
3 answers
 ([1,2,3] & [3,5]).any? 
+8
May 20, '10 at 15:23
source share
— -

Technically answered:

 class Array def non_empty? !self.empty? end end puts [1].non_empty? 

Although .any? already exists for this purpose (see JHurra answer)

+4
May 20, '10 at 15:21
source share

An equivalent query will ask if it is an array. Equivalent to !array.blank? equal to array.present?

Check out http://api.rubyonrails.org/classes/Object.html#M000280

+2
May 20, '10 at 15:32
source share



All Articles