的Ruby / Rails:如何确定一个数组包含另一个阵列中的所有元素? [英] Ruby/Rails: How to determine if one array contains all elements of another array?
本文介绍了的Ruby / Rails:如何确定一个数组包含另一个阵列中的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设:
A1 = [5,1,6,14,2,8]
我想,以确定它是否包含的所有元素:
A2 = [2,6,15]
在这种情况下,结果是假
。
有没有内置的Ruby / Rails的方法来确定这样的阵列包含?
实现的一个方法是:
a2.index {| X | !a1.include?(X)} .nil?
有没有更好的,更具可读性,方式是什么?
解决方案
A = [5,1,6,14,2,8]
B = [2,6,15]一个 - B
= GT; [5,1,14日,8]b - 一个
= GT; [15](B - A).empty?
= GT;假
Given:
a1 = [5, 1, 6, 14, 2, 8]
I would like to determine if it contains all elements of:
a2 = [2, 6, 15]
In this case the result is false
.
Is there any built-in Ruby / Rails method to identify such array inclusion ?
One way to implement this is:
a2.index{ |x| !a1.include?(x) }.nil?
Is there a better, more readable, way ?
解决方案
a = [5, 1, 6, 14, 2, 8]
b = [2, 6, 15]
a - b
=> [5, 1, 14, 8]
b - a
=> [15]
(b - a).empty?
=> false
这篇关于的Ruby / Rails:如何确定一个数组包含另一个阵列中的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文