Hi,
In message "Inconsistent behavior in Array methods"
on 03/02/21, "Warren Brown" <wkb / airmail.net> writes:
| I recently tracked down a bug in my code to a line that read:
|
| cards = cards.sort.uniq
|
| This seemed perfectly reasonable - the cards array contained Card
|objects which were Enumerable. However, the bug was due to the fact that
|Array#uniq does not use the object's == method to do comparisons - even
|though Array#sort uses the object's <=> method. Upon investigation, I found
|that the implementation of Array#uniq uses a hash, and since the key to the
|hash is the object ID, uniqueness ends up being defined as (obj1.id ==
|obj2.id). This was *surprising* to me.
I know, but I thought the order of execution time was more important,
especially for big arrays.
matz.