如果我想比较两个数组并创建一个内插输出字符串,如果数组 y
中的数组变量存在于 x
中,我如何获得每个匹配元素的输出?
这就是我正在尝试但未完全得到结果的方法。
x = [1, 2, 4]
y = [5, 2, 4]
x.each do |num|
puts " The number #{num} is in the array" if x.include?(y.each)
end #=> [1, 2, 4]
最佳答案
可以使用设置交集的方法&
为此:
x = [1, 2, 4]
y = [5, 2, 4]
x & y # => [2, 4]
关于ruby - 查找两个数组之间的共同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10230227/