在Ruby中获取数组的差异[duplicate]

时间:2022-10-20 22:25:59

Possible Duplicate:
diff a ruby string or array

可能的重复:diff ruby字符串或数组

I have an old array: [1, 2, 3, 4, 5], and new: [1, 2, 4, 6]

我有一个旧数组:[1,2,3,4,5]和new: [1, 2, 4, 6]

How to get difference with Ruby: that 5, 3 was removed and 6 was added?

如何与Ruby区别:5、3被删除,6被添加?

1 个解决方案

#1


85  

irb(main):001:0> a = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> b = [1, 2, 4, 6]
=> [1, 2, 4, 6]
irb(main):003:0> a - b
=> [3, 5]
irb(main):005:0> b - a
=> [6]
irb(main):006:0>

#1


85  

irb(main):001:0> a = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> b = [1, 2, 4, 6]
=> [1, 2, 4, 6]
irb(main):003:0> a - b
=> [3, 5]
irb(main):005:0> b - a
=> [6]
irb(main):006:0>