From: Tarun Yadav Date: 2010-05-16T00:26:54+09:00 Subject: Re: Hash method in ruby Hi Sebastian, I understand the case where I call has method on new Object. But In the first case I tried these two ways: a. A.hash b. A.class.hash Over here Both must call hash method on the Class's object(according to my understanding). But the value returned in both cases are different. Can you please help me with this? Regards Tarun Sebastian Hungerecker wrote: > On 15.05.2010 12:45, Tarun Yadav wrote: >> 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. -- Posted via http://www.ruby-forum.com/.