226

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
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Tyler DeWitt
  • 23,366
  • 38
  • 119
  • 196

13 Answers13

436
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)

Finn
  • 1,510
  • 13
  • 22
dizzy42
  • 4,826
  • 2
  • 15
  • 8
110

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.

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • Upvote for the style guide. Just the reading material I needed. – pyronaur Apr 23 '13 at 22:23
  • 1
    readable for one statement. if many then better use negate ! or add your own method to ruby array class. – Renars Sirotins May 26 '14 at 12:17
  • 5
    Prefer this to the exclude? answer as it's pure Ruby. – dscher Jan 08 '16 at 19:28
  • 1
    But still weird when working with a compound condition. `if flag unless @players.include?(p.name)` is awkward and `if flag && !@players.include?(p.name)` uses negation. – gabe Jan 15 '16 at 20:55
  • 1
    While `if` let only `true` pass the condition, `unless` lets pass `false` and `nil`. This sometimes lead to hard to find bugs. Therefore I prefer `exclude?` – ToTenMilan Oct 24 '17 at 08:00
19

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35
13

How about the following:

unless @players.include?(p.name)
  ....
end
Community
  • 1
  • 1
ilasno
  • 714
  • 1
  • 13
  • 31
  • Man, with the speed that answers come in on this site, i don't know if i'll get any reputation without some good-ole nepotism! :-D The Style Guide link is a nice touch. – ilasno Apr 27 '12 at 18:01
  • 2
    It's not about being fast. It's about being thorough. (I've found) =) – Charles Caldwell Apr 27 '12 at 18:11
8
module Enumerable
  def does_not_include?(item)
    !include?(item)
  end
end

Ok, but seriously, the unless works fine.

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • 2
    +1 `unless` it's ok for the snippet showed, but the condition may be more complex. I think it's handy to have these negated methods, they allow more declarative code. – tokland Apr 27 '12 at 18:22
6

Use unless:

unless @players.include?(p.name) do
  ...
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
6

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).!
Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
3

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.

malioboro
  • 3,097
  • 4
  • 35
  • 55
KarmaDeli
  • 610
  • 1
  • 6
  • 16
2

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
tekuri
  • 146
  • 9
1

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

  • It just needs the use of Active Support's Core Extensions. https://guides.rubyonrails.org/v3.2/active_support_core_extensions.html#exclude – the Tin Man Feb 26 '20 at 08:55
1

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
notsoscottishscot
  • 350
  • 1
  • 4
  • 11
  • I don't like it in this context but I'm genuinely surprised that you can use `not` to substitute `!`. Been using Ruby for 15 years and that's new to me. Thanks for posting! – Joshua Pinter Jun 14 '23 at 16:45
0

Try something like this:

@players.include?(p.name) ? false : true
Dorian
  • 22,759
  • 8
  • 120
  • 116
ckshei
  • 11
0

It's not a single method, but chaining count and zero works:

[1, 2, 3].count(1).zero? # => false
[1, 2, 3].count(4).zero? # => true

It also works with strings:

"hello".count("l").zero? # => false
"hello".count("q").zero? # => true
Joel
  • 61
  • 3