ehlo.
> | But why the procedure is so complex? Why not just use 'operator <=>'?
> | What I mean is that writing a good hash function may be not a simple
> | task, but most keys can be ordered by <=>.
> Do you mean you're proposing binary search instead of hash algorithm
> for Hash objects, if keys can be orderd by "<=>"? I'm afraid it would
> comprecate things even worse.
Actually, I already changed my mind. I was too binded to C++ world,
and forgot the fact that each Hash can have keys of various classes,
so it is impossible to compare them by using <=> or eql?.
What I cannot get is why Object::hash cannot be used for key? I had to
define my own hash() to force key to be found; why cannot Object::hash
compute hash based on all object variables? This would simplify things
a bit.
I.e.:
============================
class Key
def initialize (x,y)
@x, @y = x,y
end
def eql? (rhl)
@x <=> rhl.x || @y <=> rhl.y
end
attr_reader :x, :y
end
key1 = Key.new(1,2)
key2 = Key.new(1,2)
print "hash1=#{key1.hash} hash1=#{key2.hash}\n"
============================
prints:
hash1=84008036 hash1=84008024
This is result of Object::hash() call. Can Object::hash() use only
values of object variables (@x and @y) when computing hash?
dozen