summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-29 05:54:21 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-29 05:54:21 (GMT)
commit48f9012df931de3a526db7d3ce03c408a2c11ba5 (patch)
tree78fd985983a013d2004565c739bee3cf52cd2f1d
parentefc7a3a712f84e3f59f09a89a82f2c36081f0f43 (diff)
hash.c: fix symbol hash
* hash.c (rb_sym_hash): return same value as rb_any_hash() of Symbol. [Bug #9381] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51426 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--hash.c15
-rw-r--r--string.c3
-rw-r--r--test/ruby/test_hash.rb1
4 files changed, 23 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 35a8665..8a431c7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,7 @@
-Wed Jul 29 14:53:32 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+Wed Jul 29 14:54:16 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * hash.c (rb_sym_hash): return same value as rb_any_hash() of
+ Symbol. [Bug #9381]
* hash.c (rb_any_hash): fix Float hash. rb_dbl_hash() returns a
Fixnum, but not a long. [Bug #9381]
diff --git a/hash.c b/hash.c
index 9ae6d95..e852570 100644
--- a/hash.c
+++ b/hash.c
@@ -129,6 +129,21 @@ rb_hash(VALUE obj)
long rb_objid_hash(st_index_t index);
+VALUE
+rb_sym_hash(VALUE sym)
+{
+ st_index_t hnum;
+
+ if (STATIC_SYM_P(sym)) {
+ sym >>= (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
+ hnum = rb_objid_hash((st_index_t)sym);
+ }
+ else {
+ hnum = RSYMBOL(sym)->hashval;
+ }
+ return LONG2FIX(hnum);
+}
+
static st_index_t
rb_any_hash(VALUE a)
{
diff --git a/string.c b/string.c
index bf07d97..0a8f4e1 100644
--- a/string.c
+++ b/string.c
@@ -9149,6 +9149,8 @@ rb_to_symbol(VALUE name)
return rb_str_intern(name);
}
+VALUE rb_sym_hash(VALUE);
+
/*
* A <code>String</code> object holds and manipulates an arbitrary sequence of
* bytes, typically representing characters. String objects may be created
@@ -9311,6 +9313,7 @@ Init_String(void)
rb_undef_method(CLASS_OF(rb_cSymbol), "new");
rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in symbol.c */
+ rb_define_method(rb_cSymbol, "hash", rb_sym_hash, 0); /* in hash.c */
rb_define_method(rb_cSymbol, "==", sym_equal, 1);
rb_define_method(rb_cSymbol, "===", sym_equal, 1);
rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index d4837ca..c37e072 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -1282,6 +1282,7 @@ class TestHash < Test::Unit::TestCase
bad = [
5, true, false, nil,
0.0, 1.72723e-77,
+ :foo, "dsym_#{self.object_id.to_s(16)}_#{Time.now.to_i.to_s(16)}".to_sym,
].select do |x|
hash = {x => bug9381}
hash[wrapper.new(x)] != bug9381