From: "Shot (Piotr Szotkowski)" Date: 2008-05-12T08:20:28+09:00 Subject: Re: Array#uniq with Hash elements... can't remove duplicates --NPWyolIJAVLYbHY6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable andrea: > I've read on the pickaxe book that Array#uniq detects duplicates using > the eql? method on the elements, but it doesn't seem to work even if > I monkeypatch the Hash class: > class Hash > def eql? other > self =3D=3D other > end > end Hashes of the objects must also match: shot@devielle:~$ irb >> class Hash >> def hash >> to_a.hash >> end >> alias eql? =3D=3D ?> end =3D> nil >> a =3D {:foo =3D> 'bar'} =3D> {:foo=3D>"bar"} >> b =3D {:foo =3D> 'bar'} =3D> {:foo=3D>"bar"} >> [a,b].uniq =3D> [{:foo=3D>"bar"}] That said, monkeypatching Hash in this way is a (very) bad idea. -- Shot --=20 Perl is more an attempt to prove strong AI by making a language so ambiguous that humans can't parse it, yet sufficiently unambiguous that computers *can*. The day after they succeed, the human era will be ended. -- Nix --NPWyolIJAVLYbHY6 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFIJ3rxi/mCfdEo8UoRAnN2AJ4zzv7adfiamF//wnXcd+2JKvx4eQCcDMyu J3hccprppWrqHnqJF4ELKXA= =TDUR -----END PGP SIGNATURE----- --NPWyolIJAVLYbHY6--