我有两个Ruby数组,我需要看看它们是否有共同的值.我可以循环遍历一个数组中的每个值,并包含?另一方面,我相信还有更好的办法.这是怎么一回事?(两个数组都包含字符串.)
谢谢
我有两个Ruby数组,我需要看看它们是否有共同的值.我可以循环遍历一个数组中的每个值,并包含?另一方面,我相信还有更好的办法.这是怎么一回事?(两个数组都包含字符串.)
谢谢
a1 & a2
下面是一个例子:
> a1 = [ 'foo', 'bar' ]
> a2 = [ 'bar', 'baz' ]
> a1 & a2
=> ["bar"]
> !(a1 & a2).empty? # Returns true if there are any elements in common
=> true