Forum: Ruby duplicate keys in a Hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9a46755c817121b0571a563e5ab02090?d=identicon&s=25 Brian Buckley (brian)
on 2006-03-22 21:56
(Received via mailing list)
In the code below it appears Ruby is permitting duplicate keys.  What's
happening?
--Brian

h = {}
h[{}] = "test1" #key is an empty hash
h[{}] = "test2" #2nd key is an empty hash  -- two duplicate keys?
h.size # 2

{} == {}  # => true empty hashes are equal
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-03-22 23:26
(Received via mailing list)
On Thu, 2006-03-23 at 05:56 +0900, Brian Buckley wrote:
> In the code below it appears Ruby is permitting duplicate keys.  What's
> happening?
> --Brian
>
> h = {}
> h[{}] = "test1" #key is an empty hash
> h[{}] = "test2" #2nd key is an empty hash  -- two duplicate keys?
> h.size # 2
>
> {} == {}  # => true empty hashes are equal

{}.eql? {}
# => false

{}.hash == {}.hash
# => false

I don't think Hash uses == at all.
B6e200953f671dd7d33380b8e507b796?d=identicon&s=25 Eric Kidd (Guest)
on 2006-03-22 23:37
(Received via mailing list)
On Mar 22, 2006, at 3:56 PM, Brian Buckley wrote:
> {} == {}  # => true empty hashes are equal
The two empty hashes are equal, but they're not the same empty hash,
if that makes sense. Try this:

irb(main):013:0> {}.object_id == {}.object_id
=> false

I hope this answers your question!

Cheers,
Eric
This topic is locked and can not be replied to.