Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Where is the #hash method defined?

Reply
Thread Tools

Where is the #hash method defined?

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      06-08-2011
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
<(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Anurag Priyam
Guest
Posts: n/a
 
      06-08-2011
> 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.

--
Anurag Priyam
http://about.me/yeban/

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      06-08-2011
2011/6/9 Anurag Priyam <(E-Mail Removed)>:
>> 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 # =3D> Kernel


Thanks, I didn't know the #owner method


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


It's strange that the #hash method is not defined within the Kernel doc:

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

--=20
I=C3=B1aki Baz Castillo
<(E-Mail Removed)>

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      06-08-2011

On Jun 8, 2011, at 16:12 , I=F1aki Baz Castillo wrote:

> 2011/6/9 Anurag Priyam <(E-Mail Removed)>:
>>> 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?

>>=20
>> method(:hash).owner # =3D> Kernel

>=20
> Thanks, I didn't know the #owner method
>=20
>=20
>> I think the above tip was discussed on the list sometime back.

>=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.
...


 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      06-08-2011
2011/6/9 Ryan Davis <(E-Mail Removed)>:
> % 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
<(E-Mail Removed)>

 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      06-09-2011
2011/6/9 Piotr Szotkowski <(E-Mail Removed)>:
>> 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 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 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/ru...object.c#L2516
> =E2=86=92 https://github.com/ruby/ruby/blob/ru...2/object.c#L99


I see. Ok. Thanks a lot.

--=20
I=C3=B1aki Baz Castillo
<(E-Mail Removed)>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
invoke a method by reflectionthe method's parameters can not be ArrayList? jerry051 ASP .Net 2 08-02-2005 10:35 AM
BC30289: Statement cannot appear within a method body. End of method assumed. Carlos Oliveira ASP .Net 0 08-19-2004 07:51 PM
Difference between Delete method and RemoveRow method CW ASP .Net 0 04-01-2004 01:07 AM
ASP.NET: BC30289: Statement cannot appear within a method body. End of method assumed. Mike Wilmot ASP .Net 0 12-15-2003 07:49 PM



Advertisments