On Fri, Dec 30, 2011 at 15:02, Robert K.
[email protected] wrote:
On Fri, Dec 30, 2011 at 12:47 AM, Nikolai W. [email protected] wrote:
I seem to have failed in communicating what I’m after. I wasn’t after
different ways of implementing #hash, I was after the golden standard
of #hash implementations for value objects. So, again, what’s the
standard way of implementing #hash for value objects in Ruby?
I have covered this a bit earlier:
http://blog.rubybestpractices.com/posts/rklemme/018-Complete_Class.html
Boils down to that the hash code should be derived from hash codes of
all fields used as key fields in determining equivalence.
Precisely. And how do you get a single value from the hash values of
those fields?
One suggestion would be the XOR of the hash values of the object’s
class and its instance variables.
Not good, because then the same value in different fields has
identical influence on the hash code. Consequently likelihood of
collisions increases and thus performance of Hash storage and
retrieval may decrease.
This is exactly what you suggest in your article, with the addition
(or rather XOR) of the hash value of the object’s class.
But still, I don’t see the need. Note also that a proper Hash key
usually should be immutable because changing them causes all sorts of
trouble if not done carefully.
Hence the use of “value object” in my question.
And most of the time Hash keys are
String, Symbol, Fixnum and the like - which all do have their #hash
implementation already.
But how do you combine them? That’s what this whole thread has been
about. I realize now that I made quite a few assumptions about what I
thought readers would understand from my original question that may
not have been as obvious to them as they were to me. First, let me
apologize for this lack of clarity. Second, let me rephrase my
question and add some additional context and examples:
What algorithm should one employ in the calculation of the hash value
of an arbitrary value object?
As an example, what algorithm should one employ in the calculation of
the hash value of an immutable class A containing three immutable
instance variables @a, @b, @c that contain, respectively, a String, a
Fixnum, and a Symbol and that are all used in the calculation of #==?
The semi-standard solutions seem to be
class A
def hash
@a ^ @b ^ @c
end
end
and
class A
def hash
[@a, @b, @c].hash
end
end
These are the two main implementations in the Standard Library,
anyway. The first is also the solution proposed by Robert K. in
http://blog.rubybestpractices.com/posts/rklemme/018-Complete_Class.html
I would claim that the algorithm should take the class of the object
into account as well, both for consistency with #== (which should
check equality of the classes of the objects being compared) and for
added entropy.
Internally, Ruby (primarily) uses three C functions for the
calculation of combined hash values, namely rb_hash_start,
rb_hash_uint, and rb_hash_end. As an example, the hash value of a
Struct is calculated (in Ruby with these three functions wrapped in an
imaginary module C) as
class Struct
def hash
C.rb_hash_end(reduce(C.rb_hash_start(self.class.hash)){ |h, v|
C.rb_hash_uint(h, v.hash) })
end
end
Might it be useful to have Ruby expose a way to perform this
calculation from the Ruby realm so that other classes may employ this
algorithm?