From: Mark Gargan Date: 2007-06-18T20:24:40+09:00 Subject: Re: Which comparator to override 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/ -- Posted via http://www.ruby-forum.com/.