Get difference of arrays in Ruby

ArraysRuby

Arrays Problem Overview


> Possible Duplicate:
> diff a ruby string or array

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

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

Arrays Solutions


Solution 1 - Arrays

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKirView Question on Stackoverflow
Solution 1 - ArraysDogbertView Answer on Stackoverflow