From: tommy@tmtm•org
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:49187] [Ruby trunk - Bug #11381] [Open] String のサブクラスをハッシュのキーに指定した時に hash メソッドが呼ばれない
Date: Tue, 21 Jul 2015 14:23:58 +0000 [thread overview]
Message-ID: <redmine.issue-11381.20150721142357.ea74b3ed0722cb5b@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11381.20150721142357@ruby-lang.org
Issue #11381 has been reported by Masahiro Tomita.
----------------------------------------
Bug #11381: String のサブクラスをハッシュのキーに指定した時に hash メソッドが呼ばれない
https://bugs.ruby-lang.org/issues/11381
* Author: Masahiro Tomita
* Status: Open
* Priority: Normal
* Assignee:
* ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
String のサブクラスとして大文字小文字を同一視するようなクラスを作ろうとしましたが、
そのオブジェクトをハッシュのキーに指定しても期待通りに動作しませんでした。
どうやら hash メソッドが呼ばれていないようです。
```ruby
class CIString < String
def eql?(other)
self.casecmp(other) == 0
end
def hash
self.to_s.downcase.hash
end
end
h = {}
k1 = CIString.new("hoge")
k2 = CIString.new("HOGE")
p k1.eql? k2 #=> true
p k1.hash == k2.hash #=> true
h[k1] = 1
h[k2] = 2
p h #=> {"hoge"=>1, "HOGE"=>2}
```
ちなみに eql? の方はちゃんと呼ばれるようで、次のようにすると同じ値であっても別のキーとみなされます。
```ruby
class CIString < String
def eql?(other)
false
end
end
h = {}
k1 = CIString.new("hoge")
k2 = CIString.new("hoge")
h[k1] = 1
h[k2] = 2
p h #=> {"hoge"=>1, "hoge"=>2}
```
次のパッチで期待通りにサブクラスの hash メソッドが呼びだされました。
```diff
diff --git a/hash.c b/hash.c
index 7b8733f..26e5a3d 100644
--- a/hash.c
+++ b/hash.c
@@ -145,7 +145,7 @@ rb_any_hash(VALUE a)
}
hnum = rb_objid_hash((st_index_t)a);
}
- else if (BUILTIN_TYPE(a) == T_STRING) {
+ else if (BUILTIN_TYPE(a) == T_STRING && RBASIC(a)->klass == rb_cString) {
hnum = rb_str_hash(a);
}
else if (BUILTIN_TYPE(a) == T_SYMBOL) {
```
--
https://bugs.ruby-lang.org/
next parent reply other threads:[~2015-07-21 13:54 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-11381.20150721142357@ruby-lang.org>
2015-07-21 14:23 ` tommy [this message]
2019-07-05 3:23 ` [ruby-dev:50809] [Ruby master Bug#11381] String のサブクラスをハッシュのキーに指定した時に hash メソッドが呼ばれない merch-redmine
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.issue-11381.20150721142357.ea74b3ed0722cb5b@ruby-lang.org \
--to=ruby-dev@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).