From cd6414267feba67f039d0a419ae91ae50ea26cd7 Mon Sep 17 00:00:00 2001 From: akr Date: Sat, 7 Jun 2008 16:19:47 +0000 Subject: * hash.c (hash_i): make Hash#hash order insensitive. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16880 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ hash.c | 11 ++++++----- test/ruby/test_hash.rb | 4 ++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index f33053f933..e33c84eddc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sun Jun 8 01:15:11 2008 Tanaka Akira + + * hash.c (hash_i): make Hash#hash order insensitive. + (rb_hash_dup): use DUPSETUP. + Sat Jun 7 23:47:35 2008 Akinori MUSHA * ext/zlib/zlib.c (rb_deflate_initialize, Init_zlib): Fix up diff --git a/hash.c b/hash.c index b6dc7bd66c..4232ceeadf 100644 --- a/hash.c +++ b/hash.c @@ -231,14 +231,16 @@ rb_hash_new(void) VALUE rb_hash_dup(VALUE hash) { - VALUE ret = hash_alloc(RBASIC(hash)->klass); + NEWOBJ(ret, struct RHash); + DUPSETUP(ret, hash); + if (!RHASH_EMPTY_P(hash)) - RHASH(ret)->ntbl = st_copy(RHASH(hash)->ntbl); + ret->ntbl = st_copy(RHASH(hash)->ntbl); if (FL_TEST(hash, HASH_PROC_DEFAULT)) { FL_SET(ret, HASH_PROC_DEFAULT); } - RHASH(ret)->ifnone = RHASH(hash)->ifnone; - return ret; + ret->ifnone = RHASH(hash)->ifnone; + return (VALUE)ret; } static void @@ -1470,7 +1472,6 @@ hash_i(VALUE key, VALUE val, int *hval) { if (key == Qundef) return ST_CONTINUE; *hval ^= rb_hash(key); - *hval *= 137; *hval ^= rb_hash(val); return ST_CONTINUE; } diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index c3acf14e3c..c86cf3297d 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -830,4 +830,8 @@ class TestHash < Test::Unit::TestCase #assert_equal("bar", h[a]) assert_nil(h["foo"]) end + + def test_hash_hash + assert_equal({0=>2,11=>1}.hash, {11=>1,0=>2}.hash) + end end -- cgit v1.2.3