# Array comparison.. why does this work

**URL:** <https://www.ruby-forum.com/t/array-comparison-why-does-this-work/87244>\
**Category:** Ruby\
**Created:** [March 1, 2007, 3:45am UTC](https://www.ruby-forum.com/t/array-comparison-why-does-this-work/87244 "2007-03-01T03:45:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [March 1, 2007, 3:45am UTC](https://www.ruby-forum.com/t/array-comparison-why-does-this-work/87244/1 "2007-03-01T03:45:40Z")

</div>

How does Ruby do Array comparision?

```auto
old_inventory = ['a', 'b', 'd']
new_inventory = ['a','e', 'c']

puts "The following files have been added: "
puts new_inventory - old_inventory
puts ""
puts "The following files have been deleted: "
puts old_inventory - new_inventory
```

Notice the output is correct so it’s not a case of element 0’s being  
compared in each array.

So…

Does Ruby sort arrays in memory first? or is a scan done 1 element at a  
time?

Thanks.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [March 1, 2007, 3:58am UTC](https://www.ruby-forum.com/t/array-comparison-why-does-this-work/87244/2 "2007-03-01T03:58:05Z")

</div>

Hi –

On Thu, 1 Mar 2007, Jean N. wrote:

> How does Ruby do Array comparision?

One element at a time.

> Notice the output is correct so it’s not a case of element 0’s being  
> compared in each array.
> 
> So…
> 
> Does Ruby sort arrays in memory first? or is a scan done 1 element at a  
> time?

You’re not doing comparison, though; you’re doing subtraction.  
Subtraction doesn’t depend on order. You can think of it as  
“without”:

[1,2,3] - [1] =\> [2,3] # [1,2,3] without 1  
[1,2,3,1] - [1] =\> [2,3] # [1,2,3,1] without 1  
[1,2,3] - [2] =\> [1,3] # [1,2,3] without 2

etc.

David

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [March 1, 2007, 5:21am UTC](https://www.ruby-forum.com/t/array-comparison-why-does-this-work/87244/3 "2007-03-01T05:21:18Z")

</div>

> You’re not doing comparison, though; you’re doing subtraction.  
> Subtraction doesn’t depend on order. You can think of it as  
> “without”:
> 
> [1,2,3] - [1] =\> [2,3] # [1,2,3] without 1  
> [1,2,3,1] - [1] =\> [2,3] # [1,2,3,1] without 1  
> [1,2,3] - [2] =\> [1,3] # [1,2,3] without 2
> 
> etc.
> 
> David

Sorry, when I mentioned comparison I meant “Under the hood”. (I realise  
‘-’ is subtractions ), but the interpreter needs to perform comparisoin  
to see if an element exists somewhere.

I was curious if it just interates through one array looking for that  
element in the second, or does it (internally) sort the array, iterate  
each array performing comparision and if a \> b skipping the equality  
determination to save CPU cycles and doing a ‘next’.

I guess what I"m saying (Makes more sense when you see pseudo code) is  
how does it work in the interpreter.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [March 1, 2007, 6:34am UTC](https://www.ruby-forum.com/t/array-comparison-why-does-this-work/87244/4 "2007-03-01T06:34:16Z")

</div>

> I was curious if it just interates through one array looking for that  
> element in the second, or does it (internally) sort the  
> array, iterate  
> each array performing comparision and if a \> b skipping the equality  
> determination to save CPU cycles and doing a ‘next’.

I would guess that it would just do an include? call on the second array  
and delete the element if it is true.

Doing the sort first would probably not be faster - a sort operation is  
not a cheap operation, and there is no guarantee that the members of an  
array are mutually sortable anyway ([“a”, 1, Object.new] for example) -  
there is no way in which the ‘-’ method would be able to rely on calling  
the ‘\<=\>’ method on its contents.

Dan.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [March 1, 2007, 7:04am UTC](https://www.ruby-forum.com/t/array-comparison-why-does-this-work/87244/5 "2007-03-01T07:04:19Z")

</div>

Daniel S. wrote:

> Doing the sort first would probably not be faster - a sort operation is  
> not a cheap operation, and there is no guarantee that the members of an  
> array are mutually sortable anyway ([“a”, 1, Object.new] for example) -  
> there is no way in which the ‘-’ method would be able to rely on calling  
> the ‘\<=\>’ method on its contents.
> 
> Dan.

Don’t forget, you can always just look at the source[1].  
It looks to me like it converts the second array to a hash, then  
iterates through the first array, checking if each element is in the  
newly created hash.  
If it is, it skips it. If not, the value goes in the new array.

-Justin

[1] [http://ruby-doc.org/core/classes/Array.src/M002261.html](http://ruby-doc.org/core/classes/Array.src/M002261.html)

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [March 1, 2007, 7:30am UTC](https://www.ruby-forum.com/t/array-comparison-why-does-this-work/87244/6 "2007-03-01T07:30:32Z")

</div>

> Don’t forget, you can always just look at the source[1].  
> It looks to me like it converts the second array to a hash, then  
> iterates through the first array, checking if each element is in the  
> newly created hash.  
> If it is, it skips it. If not, the value goes in the new array.
> 
> -Justin
> 
> [1] [http://ruby-doc.org/core/classes/Array.src/M002261.html](http://ruby-doc.org/core/classes/Array.src/M002261.html)

Awesome I didn’t now where to find the sources to read them.

Thanks!
