Ruby: Is there an opposite of include? for Ruby Arrays?

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


I've got the following logic in my code:

if !@players.include?(p.name)
  ...
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)
  ...
end

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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)

Solution 2 - Ruby on-Rails

Here you go:

unless @players.include?(p.name)
  ...
end

You might have a look at the Ruby Style Guide for more info on similar techniques.

Solution 3 - Ruby on-Rails

Looking at Ruby only:

TL;DR

Use none? passing it a block with == for the comparison:

[1, 2].include?(1)
  #=> true
[1, 2].none? { |n| 1 == n  }
  #=> false

Array#include? accepts one argument and uses == to check against each element in the array:

player = [1, 2, 3]
player.include?(1)
 #=> true

Enumerable#none? can also accept one argument in which case it uses === for the comparison. To get the opposing behaviour to include? we omit the parameter and pass it a block using == for the comparison.

player.none? { |n| 7 == n }
 #=> true 
!player.include?(7)    #notice the '!'
 #=> true

In the above example we can actually use:

player.none?(7)
 #=> true

That's because Integer#== and Integer#=== are equivalent. But consider:

player.include?(Integer)
 #=> false
player.none?(Integer)
 #=> false

none? returns false because Integer === 1 #=> true. But really a legit notinclude? method should return true. So as we did before:

player.none? { |e| Integer == e  }
 #=> true

Solution 4 - Ruby on-Rails

How about the following:

unless @players.include?(p.name)
  ....
end

Solution 5 - Ruby on-Rails

module Enumerable
  def does_not_include?(item)
    !include?(item)
  end
end

Ok, but seriously, the unless works fine.

Solution 6 - Ruby on-Rails

Use unless:

unless @players.include?(p.name) do
  ...
end

Solution 7 - Ruby on-Rails

Try this, it's pure Ruby so there's no need to add any peripheral frameworks

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

I was struggling with a similar logic for a few days, and after checking several forums and Q&A boards to little avail it turns out the solution was actually pretty simple.

Solution 8 - Ruby on-Rails

Can you use:

unless @players.include?(p.name) do
...
end

unless is opposite of if, or you may use reject.

You can reject the not-required elements:

@players.reject{|x| x==p.name}

after the getting the results you can do your implementation.

Solution 9 - Ruby on-Rails

If your objection to the !-operator is primarily that it needs to be put in front of your check and this breaks your typing flow, then there is the .! method. You just put it after the check to invert the boolean:

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

Solution 10 - Ruby on-Rails

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

Solution 11 - Ruby on-Rails

Try something like this:

@players.include?(p.name) ? false : true

Solution 12 - Ruby on-Rails

I was looking up on this for myself, found this, and then a solution. People are using confusing methods and some methods that don't work in certain situations or not at all.

I know it's too late now, considering this was posted 6 years ago, but hopefully future visitors find this (and hopefully, it can clean up their, and your, code.)

Simple solution:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTyler DeWittView Question on Stackoverflow
Solution 1 - Ruby on-Railsdizzy42View Answer on Stackoverflow
Solution 2 - Ruby on-RailsBozhidar BatsovView Answer on Stackoverflow
Solution 3 - Ruby on-RailsSagar PandyaView Answer on Stackoverflow
Solution 4 - Ruby on-RailsilasnoView Answer on Stackoverflow
Solution 5 - Ruby on-RailsJesse WolgamottView Answer on Stackoverflow
Solution 6 - Ruby on-RailsMikita BelahlazauView Answer on Stackoverflow
Solution 7 - Ruby on-RailsKarmaDeliView Answer on Stackoverflow
Solution 8 - Ruby on-RailstekuriView Answer on Stackoverflow
Solution 9 - Ruby on-RailsChristopher OezbekView Answer on Stackoverflow
Solution 10 - Ruby on-RailsMeWillWork4FoodView Answer on Stackoverflow
Solution 11 - Ruby on-RailscksheiView Answer on Stackoverflow
Solution 12 - Ruby on-RailsVoidableryzerView Answer on Stackoverflow