From: Josh Cheek Date: 2010-05-15T20:07:35+09:00 Subject: Re: Hash method in ruby --0016e6d77f118a7b9604869ffffc Content-Type: text/plain; charset=ISO-8859-1 On Sat, May 15, 2010 at 5:45 AM, 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. > > Question > a. What is the difference between these two calls. > b. In the second statement, because the state of A's object is same > every time, I suppose it should return same values. > -- > Posted via http://www.ruby-forum.com/. > > I think your confusion lies more in the object model than the hash method. A is a class, it is always the same class (it is a constant, so every time you say A, you are talking about the same class) And since classes in Ruby are objects, you are always talking about the same object. A.new is an instance of the class, in Java this would look like "new A()" so every time you say A.new, you are talking about a different object. I suppose in Java this would approximately translate to: public class A { ... public static int hashCode() { ... } public int hashCode() { ... } ... } Though I'm not sure how well that fits, since I'm not sure whether classes are objects in Java, if they aren't, then they probably can't be hash keys. --0016e6d77f118a7b9604869ffffc--