From: Piotr Szotkowski Date: 2011-06-09T19:45:42+09:00 Subject: Re: Where is the #hash method defined? --t0UkRYy7tHLRMCai Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I=C3=B1aki Baz Castillo: > Hi, any object in Ruby has the method #hash which > returns an integer unique for the object (such value > doesn't change during the lifetime of the Ruby process). Note that #hash=E2=80=99s role is to fulfil the implication a.eql?(b) =E2=86=92 a.hash =3D=3D b.hash, but it=E2=80=99s as weak as possi= ble (i.e., it would be perfectly legal, if very inefficient, for #hash to return the same result for all objects). In general, the idea is that it=E2=80=99s often possible to have a #hash method that computes a unique-ish result much faster than performing a full-blown #eql? check; in these cases it=E2=80=99s worthwhile to compare #hash results of two objects and call #eql? only if they=E2=80=99re the same (equal #hash results do not mean the objects are #eql?, but different #hash results do mean that they are not #eql?). By default, Kernel#hash uses #object_id to compute the hash, so it=E2=80=99s quite unique (I=E2=80=99m not sure what happens when #objec= t_id rolls over the Fixnum limit; #hash seems to be returning a Fixnum anyway, so in theory it can be non-unique). > I cannot find which class or module #hash method belongs to. > I expected it could be in Object class, but it's not. Where is it? It=E2=80=99s defined as Kernel#hash and in Ruby 1.9.2 implemented here: https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L2516 =E2=86=92 https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L99 =E2=80=94 Piotr Szotkowski --=20 Log rotation code. It=E2=80=99s right behind cryptography in the list of software you should never, ever write yourself. [Jeff Hodges] --t0UkRYy7tHLRMCai Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iEYEARECAAYFAk3wowwACgkQi/mCfdEo8UptGgCdHC0pXO/v9xEQoXFFAZFAUVS/ X0kAn01/PKDpIY/UASvNo0jPu13/AFcr =mu44 -----END PGP SIGNATURE----- --t0UkRYy7tHLRMCai--