Where is the #hash method defined?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki 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).

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?

Thanks a lot.


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
A

Anurag Priyam

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?

method:)hash).owner # => Kernel

I think the above tip was discussed on the list sometime back.
 
R

Ryan Davis

=20
Thanks, I didn't know the #owner method :)
=20
=20
=20
It's strange that the #hash method is not defined within the Kernel = doc:
=20
http://www.ruby-doc.org/core/classes/Kernel.html

% ri hash
...
(from ruby core)
=3D=3D=3D Implementation from Object
=
--------------------------------------------------------------------------=
----
obj.hash =3D> fixnum

=
--------------------------------------------------------------------------=
----

Generates a Fixnum hash value for this object. This function must have =
the
property that a.eql?(b) implies a.hash =3D=3D b.hash. The hash value is =
used by
class Hash. Any hash value that exceeds the capacity of a Fixnum will be
truncated before being used.
...
 
I

Iñaki Baz Castillo

2011/6/9 Ryan Davis said:
% ri hash
...
(from ruby core)
=3D=3D=3D Implementation from Object
-------------------------------------------------------------------------= -----
=C2=A0obj.hash =C2=A0 =C2=A0=3D> fixnum

-------------------------------------------------------------------------= -----

Generates a Fixnum hash value for this object. This function must have th= e
property that a.eql?(b) implies a.hash =3D=3D b.hash. The hash value is u= sed by
class Hash. Any hash value that exceeds the capacity of a Fixnum will be
truncated before being used.
...


Thanks a lot. However it's not documented in Object class rdoc:

http://www.ruby-doc.org/core/classes/Object.html

a bug in the doc?

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
I

Iñaki Baz Castillo

2011/6/9 Piotr Szotkowski said:
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 pos= sible
(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 compa= re
#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 #obj= ect_id
rolls over the Fixnum limit; #hash seems to be returning
a Fixnum anyway, so in theory it can be non-unique).


Thanks for the very good explanation.


I see. Ok. Thanks a lot.

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top