From: "Iñaki Baz Castillo" Date: 2011-06-09T20:29:37+09:00 Subject: Re: Where is the #hash method defined? 2011/6/9 Piotr Szotkowski : >> 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: > https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L2516 > → https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L99 I see. Ok. Thanks a lot. -- Iñaki Baz Castillo