From: Sebastian Hungerecker Date: 2010-05-15T21:40:59+09:00 Subject: Re: Hash method in ruby On 15.05.2010 12:45, Tarun Yadav wrote: > Hello group, > > I have been working in java and ruby is new to me. While trying some > hands on, I could not understand the Hashing done in ruby. > > I created a class A and called hash function on the class. > > 1> A.hash > 2> A.new.hash > > While A.hash was giving same Fixnum every time, second statement > generated different values. > Note that this behaves exactly the same in java: public class A { public static void main (String [] args){ System.out.println(A.class.hashCode()); System.out.println(A.class.hashCode()); System.out.println(new A().hashCode()); System.out.println(new A().hashCode()); } } A.class.hashCode() will return the same hash code every time, while new A().hashCode() will return a different hash code each time. This is because (in ruby as well as in java) the default hash code implementation is based on object identity, meaning different objects (even if all their instance variables are the same) have different hash codes. Since by doing new A / A.new you're creating a new object each time, you get different hash codes. However when doing A.class.hashCode() / A.hash you're calling hashCode()/hash on the same object of class Class each time.