Where is the #hash method defined?

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.

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.

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.

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:

ruby/object.c at ruby_1_9_2 · ruby/ruby · GitHub

— Piotr S.

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.

2011/6/9 Piotr S. [email protected]:

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:
https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L2516
ruby/object.c at ruby_1_9_2 · ruby/ruby · GitHub

I see. Ok. Thanks a lot.

2011/6/9 Anurag P. [email protected]:

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

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

2011/6/9 Ryan D. [email protected]:

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

a bug in the doc?

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

% ri hash

(from ruby core)
=== Implementation from Object

obj.hash => fixnum