N
Nate Smith
Hello, I'm trying to redefine the way a hash works for an object so that
other types will hash to the same bucket. For example:
class A
attr :name, true
def eql?( another )
return true if self.hash == another.hash
end
def hash
@name.hash
end
def to_s
@name.to_s
end
end
a = A.new
h = Hash.new
a.name = "Bob"
h[ a ] = "foo"
print "Value: " + h[ "a" ].to_s + "\n"
The values of "a".hash and of a.hash are the same value (100), so
wouldn't h[ "a" ] be the same as h[ a ]?
The result of running the above code is: "Value: " (h[ "a" ] returns
nil). Anyone have any suggestions on how I can make this work? Thanks
much.
Nate
other types will hash to the same bucket. For example:
class A
attr :name, true
def eql?( another )
return true if self.hash == another.hash
end
def hash
@name.hash
end
def to_s
@name.to_s
end
end
a = A.new
h = Hash.new
a.name = "Bob"
h[ a ] = "foo"
print "Value: " + h[ "a" ].to_s + "\n"
The values of "a".hash and of a.hash are the same value (100), so
wouldn't h[ "a" ] be the same as h[ a ]?
The result of running the above code is: "Value: " (h[ "a" ] returns
nil). Anyone have any suggestions on how I can make this work? Thanks
much.
Nate