Forum: Ruby Which comparator to override

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5860439cab5dd9f238c8555a7f9bcbdd?d=identicon&s=25 Mark Gargan (markgargan)
on 2007-06-18 12:27
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/
5860439cab5dd9f238c8555a7f9bcbdd?d=identicon&s=25 Mark Gargan (markgargan)
on 2007-06-18 13:24
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 Gargan 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/
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-06-18 13:26
(Received via mailing list)
On 18.06.2007 12:27, Mark Gargan 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
This topic is locked and can not be replied to.