开发者

Ruby non_empty? method

开发者 https://www.devze.com 2022-12-30 20:23 出处:网络
I want to use expression: !([1,2,3] & [43,5]).empty? => false !([1,2,3] & [3,5]).empty? => true

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

0

精彩评论

暂无评论...
验证码 换一张
取 消