Permalink
Browse files

Instead of looping through an enumerable twice to determine equality,…

… only do it once
  • Loading branch information...
1 parent 21325f3 commit 3b501e1a9628bfcc90950393aa947b575bbf5231 @mcmire committed Jan 21, 2011
Showing with 15 additions and 8 deletions.
  1. +15 −8 lib/super_diff/differ.rb
View
@@ -6,20 +6,27 @@ def initialize
def diff(expected, actual)
expected_type = type_of(expected)
actual_type = type_of(actual)
- data = {
- :equal => (expected == actual),
- :expected => {:value => expected, :type => expected_type},
- :actual => {:value => actual, :type => actual_type},
- :common_type => (expected_type if expected_type == actual_type)
- }
- if expected.class == actual.class
+ same_type = (expected_type == actual_type)
+ data = {}
+ if same_type && expected.class < Enumerable
if expected.class == Array
+ equal = true
data[:breakdown] = []
(0...expected.size).each do |i|
- data[:breakdown] << [i, diff(expected[i], actual[i])]
+ diff = diff(expected[i], actual[i])
+ data[:breakdown] << [i, diff]
+ equal &&= diff[:equal]
end
end
+ else
+ equal = (expected == actual)
end
+ data.merge!(
+ :equal => equal,
+ :expected => {:value => expected, :type => expected_type},
+ :actual => {:value => actual, :type => actual_type},
+ :common_type => (expected_type if same_type)
+ )
data
end

0 comments on commit 3b501e1

Please sign in to comment.