【发布时间】:2010-03-03 03:14:05
【问题描述】:
还有其他simple,nicer的方式吗?
require 'pp'
a1 = ["02/28/10","Webinars","131","0","26 Feb 2010","0","3d, 8h, 49m, 18s"]
a2 = ["02/20/10","Webinars","131","9","26 Feb 2010","0","3d, 8h, 49m, 18s"]
def compare(array1,array2,ignore)
tmp1 = Array.new
tmp2 = Array.new
0.upto(array1.length-1) {|index|
if !ignore.include?(index)
tmp1 << array1[index]
tmp2 << array2[index]
end
}
if tmp1 == tmp2
return true
else
return false
end
end
pp a1
pp a2
puts
puts compare(a1,a2,[0,3])
输出是
["02/28/10", "Webinars", "131", "0", "26 Feb 2010", "0", "3d, 8h, 49m, 18s"]
["02/20/10", "Webinars", "131", "9", "26 Feb 2010", "0", "3d, 8h, 49m, 18s"]
true
【问题讨论】:
-
仅供参考,问题中的版本始终返回 true,因为 tmp1 和 tmp2 指向同一个数组。尝试比较([0],[1],[])
-
感谢您指出这一点。已修复。
标签: ruby arrays comparison