有包含的反义词吗?对于 Ruby 数组? [英] Is there an opposite of include? for Ruby Arrays?

查看:39
本文介绍了有包含的反义词吗?对于 Ruby 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有以下逻辑:

I've got the following logic in my code:

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

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

@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

推荐答案

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

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

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

来源:主动支持核心扩展(Rails 指南)

这篇关于有包含的反义词吗?对于 Ruby 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆