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.
On Sat, May 15, 2010 at 5:45 AM, Tarun Y.
[email protected]wrote:
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.
On 15.05.2010 12:45, Tarun Y. 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.
2010/5/15 Tarun Y. [email protected]:
But the value returned in both cases are different.
The class of A is “Class” (which is itself an object different from
A). So it’s perfectly normal that the hash method returns an other
value.
Try A.object_id and A.class.object_id:
Array.object_id
=> 2148226700
Array.class
=> Class
Array.class.object_id
=> 2148257020
Class.object_id
=> 2148257020
Array.class and Class are the same object.
Cheers,
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 H. wrote:
On 15.05.2010 12:45, Tarun Y. 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.
Thanks Jean,
Can you please confirm the difference between these:
A.class.hash #calls the hash method from Class class
A.new.hash #calls the hash method from A(if overridden otherwise method
from Object)
A.hash #? not able to understand what hash method is called. What kind
of object is A?
Regards
Tarun
Jean-Julien F. wrote:
2010/5/15 Tarun Y. [email protected]:
But the value returned in both cases are different.
The class of A is “Class” (which is itself an object different from
A). So it’s perfectly normal that the hash method returns an other
value.
Try A.object_id and A.class.object_id:
Array.object_id
=> 2148226700
Array.class
=> Class
Array.class.object_id
=> 2148257020
Class.object_id
=> 2148257020
Array.class and Class are the same object.
Cheers,
On 15.05.2010 17:55, Tarun Y. wrote:
of object is A?
A is an object of class Class. Thusly A.class returns Class.
Class is also an object of type class, but a different one,
so it has a different hash code.
Note that:
A
=> A
A.class
=> Class
A == A.class
=> false
A.class == Class
=> true
A.class.hash == Class.hash
=> true
A == Class
=> false
A.hash == Class.hash
=> false
A == A.class
=> false
A.hash == A.class.hash
=> false
On Sat, May 15, 2010 at 5:55 PM, Tarun Y.
[email protected] wrote:
Thanks Jean,
Can you please confirm the difference between these:
A.class.hash #calls the hash method from Class class
A.new.hash #calls the hash method from A(if overridden otherwise method
from Object)
No. This calls the hash object on an instance of A.
A.hash #? not able to understand what hash method is called. What kind
of object is A?
It is the class A. This is different that the object instances of A
Jesus.