# Which comparator to override

**URL:** <https://www.ruby-forum.com/t/which-comparator-to-override/107123>\
**Category:** Ruby\
**Created:** [June 18, 2007, 12:27pm UTC](https://www.ruby-forum.com/t/which-comparator-to-override/107123 "2007-06-18T12:27:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [June 18, 2007, 12:27pm UTC](https://www.ruby-forum.com/t/which-comparator-to-override/107123/1 "2007-06-18T12:27:04Z")

</div>

Hey folks,

```
      I have a map of objects in my code. The objects are Alerts

```

and basically the root alert object is the key to an array of duplicate  
alert ids.  
So basically the map contains keys of Alert objects and they map to  
arrays of fixnums.

I’m having a slight problem though accessing the arrays from the map.

My Alert extends the ActiveRecord:Base object so if I say create a map,  
alertsMap, of the Alerts by their root and one of the roots has the id  
2,  
then I retrieve the same Alert from the dB.

alert = Alert.find(2)

now I have a map where the alert with the id 2 maps to an array of  
numbers,  
and also have an Alert object with id 2.

alas querying the map with the object I retrieved returns nothing.

irb(main):219:0\> alertsMap[alert]  
=\> []

In my Alert class I override the general comparison method \<=\> as such :

```
# Equality method.
# Java Equivalent to public boolean equals(Object other)
def <=>(other)
    alert_id <=> other.alert_id
end

```

as I figured that this is what the map would use to compare object  
passed in as keys based on their alert\_ids. i.e. 2 should have compared  
equally to 2.

Does anyone know if I’m doing this the right way?  
Any help would be greatly appreciated.

Thanks,  
Mark/

---

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [June 18, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/which-comparator-to-override/107123/2 "2007-06-18T13:24:40Z")

</div>

Hey folks,

```
 I apologise for this post, it turns out within my logic that I was 

```

specifying that the id of the alert was to be the key as opposed to the  
object itself.

It’s all working away now.

Thanks,  
Mark.

Mark G. wrote:

> Hey folks,
> 
> ```
> I have a map of objects in my code. The objects are Alerts
> 
> ```
> 
> and basically the root alert object is the key to an array of duplicate  
> alert ids.  
> So basically the map contains keys of Alert objects and they map to  
> arrays of fixnums.
> 
> I’m having a slight problem though accessing the arrays from the map.
> 
> My Alert extends the ActiveRecord:Base object so if I say create a map,  
> alertsMap, of the Alerts by their root and one of the roots has the id  
> 2,  
> then I retrieve the same Alert from the dB.
> 
> alert = Alert.find(2)
> 
> now I have a map where the alert with the id 2 maps to an array of  
> numbers,  
> and also have an Alert object with id 2.
> 
> alas querying the map with the object I retrieved returns nothing.
> 
> irb(main):219:0\> alertsMap[alert]  
> =\> []
> 
> In my Alert class I override the general comparison method \<=\> as such :
> 
> ```
> # Equality method.
> # Java Equivalent to public boolean equals(Object other)
> def <=>(other)
> alert_id <=> other.alert_id
> end
> 
> ```
> 
> as I figured that this is what the map would use to compare object  
> passed in as keys based on their alert\_ids. i.e. 2 should have compared  
> equally to 2.
> 
> Does anyone know if I’m doing this the right way?  
> Any help would be greatly appreciated.
> 
> Thanks,  
> Mark/

---

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [June 18, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/which-comparator-to-override/107123/3 "2007-06-18T13:26:50Z")

</div>

On 18.06.2007 12:27, Mark G. wrote:

> My Alert extends the ActiveRecord:Base object so if I say create a map,  
> alas querying the map with the object I retrieved returns nothing.  
> end
> 
> as I figured that this is what the map would use to compare object  
> passed in as keys based on their alert\_ids. i.e. 2 should have compared  
> equally to 2.
> 
> Does anyone know if I’m doing this the right way?  
> Any help would be greatly appreciated.

If your map is a Hash then you need to override #eql? and #hash. So in  
your case that could be

def hash() alert\_id end  
def eql?(o) alert\_id.eql? o.alert\_id end  
alias == eql

\<=\> is only used for sorting.

HTH

robert
