-
-
Save Vaguery/729477 to your computer and use it in GitHub Desktop.
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 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