Created
June 21, 2013 06:35
-
-
Save mockra/5829302 to your computer and use it in GitHub Desktop.
Array Diff - find additions and deletions between arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrayDiff | |
attr_reader :one, :two | |
def initialize one, two | |
@one, @two = one.to_a, two.to_a | |
end | |
def deletions | |
one_dup = one.clone | |
two.each { |x| one_dup.slice!(one_dup.index(x)) if one_dup.index(x) } | |
one_dup | |
end | |
def additions | |
two_dup = two.clone | |
one.each { |x| two_dup.slice!(two_dup.index(x)) if two_dup.index(x) } | |
two_dup | |
end | |
end | |
describe ArrayDiff do | |
let(:array_diff) { ArrayDiff.new [1, 2, 2, 3, 6, 6, 7, 7, 7], | |
[2, 3, 3, 4, 8, 9] } | |
describe '#deletions' do | |
it 'returns array of deletions' do | |
expect(array_diff.deletions).to eq [1, 2, 6, 6, 7, 7, 7] | |
end | |
end | |
describe '#additions' do | |
it 'returns array of additions' do | |
expect(array_diff.additions).to eq [3, 4, 8, 9] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment