Hashes in Sets

P

Paul Mucur

In Ruby 1.8.6-p111 (the latest version bundled with Mac OS X) and Ruby
1.8.6-p114, the Set class will duplicate any Hash elements it contains:
require 'set'
s = Set.new
s << {}
=> # said:
=> # said:
s << {:a => 1}
=> # said:
s << {:a => 1}
=> #<Set: {{:a=>1}, {}, {:a=>1}, {}}>

This seems to be due to the fact that the Set class uses a Hash
internally to store its elements as keys and the Hash class itself
does not equate two hashes with the same keys and values:
{} == {} => true
{}.eql?({}) => false
{}.hash == {}.hash
=> false

Defining hash and eql? methods for Hash seems to correct this:

class Hash
# Create a hash based on the keys and values.
def hash
"#{keys.join}#{values.join}".hash
end

# To determine whether two hashes are the same, compare
# their hashes.
def eql?(other)
hash == other.hash
end
end
{}.eql?({}) => true
{:a => 1}.eql?({:a => 1}) => true
{:b => 1, :a => 2}.eql?({:a => 2, :b => 1}) => true
s = Set.new
=> # said:
=> # said:
=> #<Set: {{}}>

This issue of Hash's eql? method has been brought up before in http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3ecd14d6e8bbc9cf/88d51de762c3dbd1
and, more recently, http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/572e8d8b01a7d24e/c5f532d868afab05

I see that it has been fixed in Ruby 1.9.0 and was wondering if that
change will also be implemented in the 1.8 series?

Thanks in advance,

-- Paul
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top