Is there an opposite of include? for Ruby Arrays?

前端 未结 11 1828
孤街浪徒
孤街浪徒 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:43

    if @players.exclude?(p.name)
        ...
    end
    

    ActiveSupport adds the exclude? method to Array, Hash, and String. This is not pure Ruby, but is used by a LOT of rubyists.

    Source: Active Support Core Extensions (Rails Guides)

提交回复
热议问题