我有一个数组a = ["1","2","3","6","7"]
和另一个数组b = ["2","4","7"]
。我想检查b
中是否存在a
的任何内容。
答案 0 :(得分:2)
这很简单:
(a & b).blank?
实际上它做了什么,它需要两个数组的intersection
并返回结果,然后检查结果是否为空/空。
答案 1 :(得分:2)
你可以做到
a = ["1","2","3","6","7"]
b = ["2","4","7"]
b.any? { |e| a.include?(e) }
答案 2 :(得分:1)
使用Ruby的&
运算符,它将返回一个值为intersection
的数组为两个数组的数组,下面是一个示例。
pry(main)> a = ["1","2","3","6","7"]
=> ["1", "2", "3", "6", "7"]
pry(main)> b = ["2","4","7"]
=> ["2", "4", "7"]
pry(main)> a & b
=> ["2", "7"]
pry(main)> (a & b).empty?
=> false
在Rails中,您还可以使用blank?
pry(main)> (a & b).blank?
=> false
希望上面的例子有帮助