Is there an opposite of include? for Ruby Arrays?

前端 未结 11 1759
孤街浪徒
孤街浪徒 2020-12-23 13:11

I\'ve got the following logic in my code:

if !@players.include?(p.name)
  ...
end

@players is an array. Is there a method so

11条回答
  •  醉梦人生
    2020-12-23 13:18

    Using unless is fine for statements with single include? clauses but, for example, when you need to check the inclusion of something in one Array but not in another, the use of include? with exclude? is much friendlier.

    if @players.include? && @spectators.exclude? do
      ....
    end
    

    But as dizzy42 says above, the use of exclude? requires ActiveSupport

提交回复
热议问题