我可以查一下包括吗?在ruby on rails数组中至少有一个元素??这里是我的例子:
a = 1234
b = [1,5,6,7,8....]
if a.include?b[0] == true
app => return true
if a.include?b[0] == true and a.include?b[1] == false and a.include?b[2] == false and ......
app still return true
在真正的应用程序中,我不能调用b[0]、b[1]、b[2]像这样,那么,我该怎么检查include呢在rails应用程序的数组中至少有一个元素?请帮帮我:)
最佳答案
a1 = a.to_s.chars.map(&:to_i)
# => [1, 2, 3, 4]
b.any? {|i| a1.include?(i) }
# => true
更新:
Ruby 2.4有一个Integer#digits所以你可以
a1 = a.digits
b.any? {|i| a1.include?(i) }