Where is the #hash method defined?

Discussion in 'Ruby' started by Iñaki Baz Castillo, Jun 8, 2011.

  1. 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
    <>
    Iñaki Baz Castillo, Jun 8, 2011
    #1
    1. Advertising

  2. > 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/
    Anurag Priyam, Jun 8, 2011
    #2
    1. Advertising

  3. 2011/6/9 Anurag Priyam <>:
    >> 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
    <>
    Iñaki Baz Castillo, Jun 9, 2011
    #3
  4. Iñaki Baz Castillo

    Ryan Davis Guest

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

    > 2011/6/9 Anurag Priyam <>:
    >>> 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.
    ...
    Ryan Davis, Jun 9, 2011
    #4
  5. 2011/6/9 Ryan Davis <>:
    > % 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
    <>
    Iñaki Baz Castillo, Jun 9, 2011
    #5
  6. 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=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/ruby_1_9_2/object.c#L2516
    > =E2=86=92 https://github.com/ruby/ruby/blob/ruby_1_9_2/object.c#L99


    I see. Ok. Thanks a lot.

    --=20
    I=C3=B1aki Baz Castillo
    <>
    Iñaki Baz Castillo, Jun 9, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  2. rp
    Replies:
    1
    Views:
    513
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    610
    David A. Black
    Jul 2, 2008
  4. Jian Lin
    Replies:
    3
    Views:
    125
    Robert Klemme
    May 20, 2010
  5. Ralf Baerwaldt
    Replies:
    1
    Views:
    129
    Paul Lalli
    Jul 20, 2004
Loading...

Share This Page