Skip to content

Instantly share code, notes, and snippets.

@Vaguery
Forked from gilesbowkett/array_elements_diff.rb
Created December 5, 2010 21:20
Show Gist options
  • Save Vaguery/729477 to your computer and use it in GitHub Desktop.
Save Vaguery/729477 to your computer and use it in GitHub Desktop.
# class Array
# def token_diff(other)
# memo = 0
# self.each_with_index do |element, index|
# memo += 1 if other[index] != element
# end
# return memo
# end
# end
# seemed to be a problem in one spec you provided; also in the method calls
require 'rspec'
class Array
def token_diff(other)
matches = (0..self.length).collect {|i| self[i].to_s == (other[i].to_s || nil)}
matches.find_all {|bool| bool==false}.length
end
end
[:a, [[:b, [:c, "d"], [:c, "e"]], [:f, "g"]]].token_diff([:a, [[:b, [:c, "d"], [:c, "e"]], [:f, "g"]]]).should == 0
[:a, [[:b, [:c, "d"], [:c, "e"]], [:f, "g"]]].token_diff([:a, [[:b, [:c, "d"], [:c, "e"]], [:f, "h"]]]).should == 1
a = [:a, [[:b, [:c, "d"], [:c, "e"]], [:f, "g"]]]
b = [:a, [[:c, "h"], [:a, [[:b, [:c, "d"], [:c, "e"]], [:f, "g"]]]]]
a.token_diff(b).should == 1
a = [:a, :b, :c, :d, :e]
b = [:a, :b]
a.token_diff(b).should == 3
a = [:a, [[:b, [:c, "d"], [:c, "e"]], [:f, "g"]]]
b = [:a, [[:c, "h"], [:a, [[:b, [:c, "d"], [:c, "e"]], [:f, "i"]]]]]
a.token_diff(b).should == 1 # was 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment