# Array Comparison

**URL:** <https://www.ruby-forum.com/t/array-comparison/65306>\
**Category:** Ruby\
**Created:** [August 17, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/array-comparison/65306 "2006-08-17T13:38:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 17, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/array-comparison/65306/1 "2006-08-17T13:38:06Z")

</div>

Hi Guys

I have compared an array and ‘0’ is being returned which indicates to  
me that that array is equal

p t = $ie.table(:index, ‘2’).to\_a.flatten  
p t \<=\> [“HINCKLEY”, “HINDHEAD”, “HINTON ST GEORGE”]

Now would it be possible for me to extract any differences from this  
array (that is, when the array is not equal)?

Or should I really be using Test::Unit: to assert array equality?

aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 17, 2006, 1:56pm UTC](https://www.ruby-forum.com/t/array-comparison/65306/2 "2006-08-17T13:56:07Z")

</div>

On 17.08.2006 13:32, aidy wrote:

> Or should I really be using Test::Unit: to assert array equality?

You can use Array#zip (block form) to determine any differences.

[“HINCKLEY”, “HINDHEAD”, “HINTON ST GEORGE”].zip(t) do |a,b|  
if a != b  
print "Difference: ", a.inspect, " ", b.inspect, “\n”  
end  
end

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 17, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/array-comparison/65306/3 "2006-08-17T14:50:15Z")

</div>

Robert K. schrieb:

> You can use Array#zip (block form) to determine any differences.
> 
> [“HINCKLEY”, “HINDHEAD”, “HINTON ST GEORGE”].zip(t) do |a,b|  
> if a != b  
> print "Difference: ", a.inspect, " ", b.inspect, “\n”  
> end  
> end

But note that this doesn’t work if the second array contains more  
elements than the first:

[1].zip [1, 2, 3] # =\> [[1, 1]]

It is also problematic if the arrays can contain nil:

[nil, nil, nil].zip [nil] # =\> [[nil, nil], [nil, nil], [nil, nil]]

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 17, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/array-comparison/65306/4 "2006-08-17T16:11:22Z")

</div>

You could always add an XOR function to the Array class.

class Array  
def XOR(i)  
return (i | self) - (i & self)  
end

def ^(i)  
return XOR(i)  
end  
end

It should be there already anyway, even if it is just syntactic sugar…

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 17, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/array-comparison/65306/5 "2006-08-17T15:05:17Z")

</div>

> Now would it be possible for me to extract any differences from this  
> array (that is, when the array is not equal)?
> 
> Or should I really be using Test::Unit: to assert array equality?
> 
> aidy

t = [2, 3, 4, 5]

# use == if you want to test for equality

p t == [2, 3, 4, 5] # =\> true

if t != [3, 4, 5, 6]

# you may use the array set methods to show the differences

p t - [3, 4, 5, 6] #=\> [2]  
p [3, 4, 5, 6] - t #=\> [6]  
p ([3, 4, 5, 6] | t) - ([3, 4, 5, 6] & t) #=\> [6, 2]  
end

cheers

Simon
