# Where is the #hash method defined?

**URL:** <https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050>\
**Category:** Ruby\
**Created:** [June 9, 2011, 12:47am UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050 "2011-06-09T00:47:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 9, 2011, 12:47am UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/1 "2011-06-09T00:47:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [June 9, 2011, 1:03am UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/2 "2011-06-09T01:03:56Z")

</div>

In ruby 1.9.2:

p method(:hash).owner

–output:–  
Kernel

Although, the 1.9.2 docs don’t reveal that.

In ruby 1.8.6:

p method(:hash).owner

–output:–  
Line 1: undefined method `owner’ for #\<Method: Object(Kernel)#hash\>  
(NoMethodError)

The error message hints that Kernel is where #hash might reside.

Object includes Kernel, so Object and Kernel should both be checked for  
any methods that all objects respond to.

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [June 9, 2011, 2:07am UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/3 "2011-06-09T02:07:03Z")

</div>

7stud – wrote in post #1004048:

> Object includes Kernel, so Object and Kernel should both be checked for  
> any methods that all objects respond to.

And of course in ruby 1.9.2, you also need to check BasicObject.

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [June 9, 2011, 12:47pm UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/4 "2011-06-09T12:47:38Z")

</div>

Iñaki Baz C.:

> 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’s role is to fulfil the implication  
a.eql?(b) → a.hash == b.hash, but it’s as weak as possible  
(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’s 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’s worthwhile to compare  
#hash results of two objects and call #eql? only if they’re 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’s quite unique (I’m not sure what happens when #object\_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’s 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>

→ [ruby/object.c at ruby\_1\_9\_2 · ruby/ruby · GitHub](https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L99)

— Piotr S.

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [June 9, 2011, 5:59pm UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/5 "2011-06-09T17:59:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [June 9, 2011, 1:30pm UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/6 "2011-06-09T13:30:15Z")

</div>

2011/6/9 Piotr S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> method that computes a unique-ish result much faster than performing  
> a full-blown #eql? check; in these cases it’s worthwhile to compare  
> #hash results of two objects and call #eql? only if they’re 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’s quite unique (I’m not sure what happens when #object\_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’s defined as Kernel#hash and in Ruby 1.9.2 implemented here:  
> [ruby/object.c at ruby\_1\_9\_2 · ruby/ruby · GitHub](https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L2516)  
> → [ruby/object.c at ruby\_1\_9\_2 · ruby/ruby · GitHub](https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L99)

I see. Ok. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [June 9, 2011, 6:15pm UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/7 "2011-06-09T18:15:19Z")

</div>

2011/6/9 Anurag P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

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](http://www.ruby-doc.org/core/classes/Kernel.html)

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [June 9, 2011, 7:01pm UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/8 "2011-06-09T19:01:43Z")

</div>

2011/6/9 Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> property that a.eql?(b) implies a.hash == 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.  
> …

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

[http://www.ruby-doc.org/core/classes/Object.html](http://www.ruby-doc.org/core/classes/Object.html)

a bug in the doc?

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [June 9, 2011, 6:41pm UTC](https://www.ruby-forum.com/t/where-is-the-hash-method-defined/208050/9 "2011-06-09T18:41:18Z")

</div>

On Jun 8, 2011, at 16:12 , Iaki Baz C. wrote:

> It’s strange that the #hash method is not defined within the Kernel doc:
> 
> [http://www.ruby-doc.org/core/classes/Kernel.html](http://www.ruby-doc.org/core/classes/Kernel.html)

## % ri hash … (from ruby core) === Implementation from Object

obj.hash =\> fixnum
