Hashing across different types

N

Nate Smith

Sorry if this was sent more than once.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top