# Comparing Arrays

**URL:** <https://www.ruby-forum.com/t/comparing-arrays/56905>\
**Category:** Ruby\
**Created:** [April 5, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/comparing-arrays/56905 "2006-04-05T18:44:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 5, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/comparing-arrays/56905/1 "2006-04-05T18:44:05Z")

</div>

I have two arrays: originalList and deleteList. I’m trying to iterate  
through original list and remove each item if it appears in the  
deleteList. Simple enough. Here’s my snippet for doing this:

originalList.each do |item|  
if deleteList.include?(item)  
puts "Deleting " + item  
originalList.delete(item)  
end  
end

Unfortunately, this code doesn’t seem to be doing what I think it  
should. It seems to skip some items in originalList. I can provide  
examples upon request. Is there anything wrong with my algorithm?

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 5, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/comparing-arrays/56905/2 "2006-04-05T18:58:34Z")

</div>

I would think that trying to remove items from the list you are  
enumerating is generally a bad thing… I don’t know enough about Ruby  
internals to know whether that applies to Ruby, but I generally avoid  
it.

Might you want this:

originalList = originalList - deleteList

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 5, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/comparing-arrays/56905/3 "2006-04-05T18:58:40Z")

</div>

On Apr 5, 2006, at 11:44 AM, Nathan O. wrote:

> I have two arrays: originalList and deleteList.

Just FYI, the Ruby naming convention is to make those original\_list  
and delete\_list.

> I’m trying to iterate  
> through original list and remove each item if it appears in the  
> deleteList. Simple enough.

Sure is, try:

original\_list - delete\_list

😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 5, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/comparing-arrays/56905/4 "2006-04-05T19:29:10Z")

</div>

On 4/5/06, Nathan O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unfortunately, this code doesn’t seem to be doing what I think it  
> should. It seems to skip some items in originalList. I can provide  
> examples upon request. Is there anything wrong with my algorithm?

I got bit by something similar this past weekend. It turns out that  
Array#delete deletes based on == comparisons (generally means two  
objects are from the same class and have the same instance variable  
values), not eql? comparisons (means same object) as I had guessed.  
That can be a significant difference. For one thing it allows the  
delete method to delete more than one object from the array. If you  
want to delete a specific object based on its identity, as far as I  
know you need to do it like this.

obj\_to\_delete = whatever  
my\_array.delete\_if { |obj| obj.eql?(obj\_to\_delete) }

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 5, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/comparing-arrays/56905/5 "2006-04-05T22:51:29Z")

</div>

On Apr 5, 2006, at 3:40 PM, Nathan O. wrote:

> As an aside, is\_this the ruby naming convention, orThis? All the  
> references I’ve seen on the web useThis. Either is fine, I’m just  
> wondering now that it’s been brought up.

vars\_and\_methods\_like\_this

ClassesAndModulesLikeThis

CONSTANTS\_GENERALLY\_LIKE\_THIS # some people use the class style here  
too

nothingLikeThisJavaStyle # 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 5, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/comparing-arrays/56905/6 "2006-04-05T22:40:37Z")

</div>

Mark V. wrote:

> I got bit by something similar this past weekend. It turns out that  
> Array#delete deletes based on == comparisons (generally means two  
> objects are from the same class and have the same instance variable  
> values), not eql? comparisons (means same object) as I had guessed.  
> That can be a significant difference. For one thing it allows the  
> delete method to delete more than one object from the array. If you  
> want to delete a specific object based on its identity, as far as I  
> know you need to do it like this.
> 
> obj\_to\_delete = whatever  
> my\_array.delete\_if { |obj| obj.eql?(obj\_to\_delete) }

That sounds like something not many people would assume, and therefor  
maybe something that might be reviewed before 2.0… but anyways!

The list1 - list2 way of doing this worked wonders. Funny, I used this  
two weeks ago, the last time I hacked on this script, and had completely  
forgotten about it. Many thanks to everyone!

As an aside, is\_this the ruby naming convention, orThis? All the  
references I’ve seen on the web useThis. Either is fine, I’m just  
wondering now that it’s been brought up.

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 5, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/comparing-arrays/56905/7 "2006-04-05T23:08:03Z")

</div>

James G. wrote:

> vars\_and\_methods\_like\_this
> 
> ClassesAndModulesLikeThis
> 
> CONSTANTS\_GENERALLY\_LIKE\_THIS # some people use the class style here  
> too
> 
> nothingLikeThisJavaStyle # 😉
> 
> James Edward G. II

Seems you’re right! I was looking at documentation for 1.6. Everything  
I’m finding for 1.8 is\_this\_way.

Wonderful. I’ll try to remember that for my _next_ project!
