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.

···

--
Iñaki Baz Castillo
<ibc@aliax.net>

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/

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

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 Szotkowski

···

--
Log rotation code. It’s right behind cryptography in the
list of software you should never, ever write yourself.
                                           [Jeff Hodges]

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:

  module Kernel - RDoc Documentation

···

2011/6/9 Anurag Priyam <anurag08priyam@gmail.com>:

--
Iñaki Baz Castillo
<ibc@aliax.net>

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

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
ruby/object.c at ruby_1_9_2 · ruby/ruby · GitHub

I see. Ok. Thanks a lot.

···

2011/6/9 Piotr Szotkowski <chastell@chastell.net>:

--
Iñaki Baz Castillo
<ibc@aliax.net>

% ri hash
...
(from ruby core)
=== Implementation from Object

···

On Jun 8, 2011, at 16:12 , Iñaki Baz Castillo wrote:

2011/6/9 Anurag Priyam <anurag08priyam@gmail.com>:

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:

module Kernel - RDoc Documentation

------------------------------------------------------------------------------
  obj.hash => fixnum

------------------------------------------------------------------------------

Generates a Fixnum hash value for this object. This function must have the
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:

  class Object - RDoc Documentation

a bug in the doc?

···

2011/6/9 Ryan Davis <ryand-ruby@zenspider.com>:

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

------------------------------------------------------------------------------

Generates a Fixnum hash value for this object. This function must have the
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.
...

--
Iñaki Baz Castillo
<ibc@aliax.net>