I’m learning Ruby. I’ve got the O’Reilly book, “The Ruby Programming Language,” which states unequivocally:
“Object class implements the hash method to simply return an object’s ID.”
I’ve also seen this assertion in other books: http://my.safaribooksonline.com/book/web-development/ruby/9780321700308/create-classes-that-understand-equality/ch12lev1sec8
But when I run this code, the two lines do not generate the same number:
myObject = Object.new
puts myObject.hash
puts myObject.object_id
So what’s the deal? I’m running Ruby 1.9.3.
The Object implementation hashes the
object_id. The value isn’t theobject_id, but theobject_idis the input to the hash function.Via https://github.com/ruby/ruby/blob/trunk/object.c#L110