Ruby non_empty? method
I want to use expression:
!([1,2,3] & [43,5]).empty?
=> false
!([1,2,3] & [3,5]).empty?
=> true
to check if two arrays contains at least one common value. And I wonder if t开发者_StackOverflowhere is a better way of doing it? Maybe something like:
([1,2,3] & [3,5]).non_empty?
How to write non_empty?
method?
([1,2,3] & [3,5]).any?
Technically answered:
class Array
def non_empty?
!self.empty?
end
end
puts [1].non_empty?
Though .any?
already seems to exist for that purpose (see JHurra's answer)
An equivalent query would be asking if the array is not blank. The equivalent to !array.blank?
is array.present?
Check http://api.rubyonrails.org/classes/Object.html#M000280
精彩评论