Tyler DeWitt Tyler DeWitt - 4 months ago 190
Ruby Question

Is there an opposite of include? for Ruby Arrays?

I've got the following logic in my code:

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


@players
is an array. Is there a method so I can avoid the
!
?

Ideally, this snippet would be:

if @players.does_not_include?(p.name) do
...
end

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

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