有包含的反义词吗?对于 Ruby 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10355477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:19:24  来源:igfitidea点击:

Is there an opposite of include? for Ruby Arrays?

ruby-on-railsruby

提问by Tyler DeWitt

I've got the following logic in my code:

我的代码中有以下逻辑:

if [email protected]?(p.name)
  ...
end

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

@players是一个数组。有没有一种方法可以避免!

Ideally, this snippet would be:

理想情况下,这个片段应该是:

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

回答by dizzy42

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.

的ActiveSupport增加的exclude?方法ArrayHashString。这不是纯 Ruby,但被很多 Ruby 爱好者使用。

Source: Active Support Core Extensions (Rails Guides)

来源:Active Support Core Extensions(Rails 指南)

回答by Bozhidar Batsov

Here you go:

干得好:

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

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

您可以查看Ruby 样式指南以获取有关类似技术的更多信息。

回答by ilasno

How about the following:

以下情况如何:

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

回答by Sagar Pandya

Looking at Ruby only:

只看 Ruby:

TL;DR

TL; 博士

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

使用none?传递一个块来==进行比较:

[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:

Array#include?接受一个参数并用于==检查数组中的每个元素:

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.

Enumerable#none?也可以接受一个参数,在这种情况下它===用于比较。为了获得相反的行为,include?我们省略了参数并将其传递给一个==用于比较的块。

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:

那是因为Integer#==Integer#===是等价的。但请考虑:

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

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

none?返回false因为Integer === 1 #=> true. 但真正合法的notinclude?方法应该返回true。所以就像我们之前所做的那样:

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

回答by Jesse Wolgamott

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

Ok, but seriously, the unless works fine.

好的,但说真的,除非工作正常。

回答by Mikita Belahlazau

Use unless:

使用unless

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

回答by tekuri

Can you use:

你能不能用:

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

unlessis opposite of if, or you may use reject.

unless与 相反if,或者您可以使用reject.

You can rejectthe not-required elements:

您可以reject使用不需要的元素:

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

after the getting the results you can do your implementation.

获得结果后,您可以进行实施。

回答by Voidableryzer

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

我知道现在为时已晚,考虑到这是 6 年前发布的,但希望未来的访问者能找到它(希望它可以清理他们和您的代码。)

Simple solution:

简单的解决方案:

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

回答by MeWillWork4Food

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

usingunless适用于带有单个include?子句的语句,但是,例如,当您需要检查某个内容是否包含在另一个中Array而不是另一个中时,使用include?withexclude?会友好得多。

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

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

但是正如上面dizzy42所说,使用exclude?需要ActiveSupport

回答by ckshei

Try something like this:

尝试这样的事情:

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