【问题标题】:Ruby: Is there an opposite of include? for Ruby Arrays?Ruby:包含的反义词有吗?对于 Ruby 数组?
【发布时间】:2012-05-08 11:42:21
【问题描述】:

我的代码中有以下逻辑:

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

@players 是一个数组。有没有办法让我避免!

理想情况下,这个 sn-p 应该是:

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

【问题讨论】:

  • do 是有效的红宝石吗?我收到一个错误syntax error, unexpected end-of-input(如果我删除了do,则有效)
  • 请参阅stackoverflow.com/a/60404934/128421,了解搜索数组与集合的基准。

标签: ruby-on-rails ruby


【解决方案1】:
if @players.exclude?(p.name)
    ...
end

ActiveSupport 将exclude? 方法添加到ArrayHashString。这不是纯 Ruby,但很多 Ruby 爱好者都在使用它。

来源:Active Support Core Extensions (Rails Guides)

【讨论】:

  • 次要备注:AS 将它添加到 Enumerable 中,因此您可以将它与包含 Enumerable 的所有类一起使用。即使是哈希。 :)
  • 这应该是绿色的勾
  • Rails 4.2.3(可能更早)不再自动加载该方法,您需要require 'active_support/core_ext/enumerable'
【解决方案2】:

给你:

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

您可以查看Ruby Style Guide,了解有关类似技术的更多信息。

【讨论】:

  • 支持风格指南。正是我需要的阅读材料。
  • 一个语句可读。如果很多,那么最好使用 negate !或将您自己的方法添加到 ruby​​ 数组类。
  • 喜欢这个而不是排除?回答,因为它是纯 Ruby。
  • 但是在使用复合条件时仍然很奇怪。 if flag unless @players.include?(p.name) 很尴尬,if flag && !@players.include?(p.name) 使用否定。
  • if 只让true 传递条件时,unlessfalsenil 传递。这有时会导致很难找到错误。所以我更喜欢exclude?
【解决方案3】:

以下内容如何:

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

【讨论】:

  • 伙计,这个网站上答案的速度如此之快,我不知道如果没有一些良好的裙带关系,我是否会获得任何声誉! :-D 样式指南链接很不错。
  • 这与快速无关。这是关于彻底。 (我找到了)=)
【解决方案4】:

只看 Ruby:

TL;DR

使用none? 传递一个带有== 的块进行比较:

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

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

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

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

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

在上面的例子中我们实际上可以使用:

player.none?(7)
 #=> true

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

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

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

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

【讨论】:

    【解决方案5】:
    module Enumerable
      def does_not_include?(item)
        !include?(item)
      end
    end
    

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

    【讨论】:

    • +1 unless 显示的 sn-p 没问题,但条件可能更复杂。我认为拥有这些否定方法很方便,它们允许更多声明性代码。
    【解决方案6】:

    使用unless:

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

    【讨论】:

      【解决方案7】:

      试试这个,它是纯 Ruby,所以不需要添加任何外围框架

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

      几天来,我一直在为类似的逻辑苦苦挣扎,在检查了几个论坛和问答板都无济于事后,结果发现解决方案实际上非常简单。

      【讨论】:

        【解决方案8】:

        你能用吗:

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

        unlessif相反,也可以使用reject

        你可以reject不需要的元素:

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

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

        【讨论】:

          【解决方案9】:

          unless 适用于具有单个 include? 子句的语句,但是,例如,当您需要检查一个 Array 中是否包含某些内容时,可以使用 include? 和 @987654326 @ 更友好。

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

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

          【讨论】:

          【解决方案10】:

          试试这样的:

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

          【讨论】:

            【解决方案11】:

            我自己查找了这个,找到了这个,然后找到了解决方案。人们正在使用令人困惑的方法和一些在某些情况下不起作用或根本不起作用的方法。

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

            简单的解决方案:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-01-25
              • 1970-01-01
              • 2015-05-24
              • 1970-01-01
              • 2011-12-10
              相关资源
              最近更新 更多