ruby - 如何检查数组的值是否包含一个或多个值?

标签 ruby arrays

我正在查看数组中是否包含一个或多个值。例如,像这样:

[1,2,3,4,5,6].include?([4,1])  # => true
[4,1,6,2].include?([4,1])  # => true
[3,4,7].include?([4,1])  # => false

当然,“包括?”方法只能检查一个值。是否有检查多个值的方法?

最佳答案

>> [1,2,3,4,5,6] & [4,1]
=> [1, 4]
>> [1,2,3,4,5,6] & [7,9]
=> []
>>

关于ruby - 如何检查数组的值是否包含一个或多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4893884/

相关文章:

ruby - 如何使用 openssl 安装 ruby​​(rubygems)?

ruby - Ruby 的集合库

javascript - 根据状态在特定表格单元格中显示 "Task"。响应式JS

ruby-on-rails - 我何时何地需要 Rails 应用程序中的文件?

ruby-on-rails - 摩卡 : How to add expectation of a method when there are multiple invocations with different parameters

arrays - 在 Struts 2 中将字符串数组作为静态参数传递

arrays - 在嵌套数组mongodb中插入对象nodejs

python - numpy 无法识别转换中的数据类型

c - 在 C 编程中使用数组、循环和条件

ruby - 使用 nokogiri 和 ruby​​zip 编辑 docx