Gauche のハッシュテーブル Ruby のハッシュテーブル

Gauche のハッシュテーブルは Key の 比較を eq?, eqv?, equal?, string=? から選択して指定します。

gosh> (hash-table 'eq? '(a . 1) '(b . 2))
#<hash-table eq? 0x89ff578>

gosh> (hash-table-get (hash-table 'eq? '(a . 1) '(b . 2)) 'a)
1

gosh> (hash-table-get (hash-table 'equal? '(a . 1) '("b" . 2)) "b")
2

Ruby は等価性を判定する述語の指定はなくて、いつも Scheme の equal? による比較を行っているイメージ。

irb(main):001:0> {1=>"a","1"=>"b"}[1]
=> "a"
irb(main):002:0> {1=>"a","1"=>"b"}["1"]
=> "b"

Gauche のハッシュテーブルは Key が存在しなかった場合は例外を発生させます。

gosh> (hash-table-get (hash-table 'equal? '(a . 1) '("b" . 2)) "c")
*** ERROR: #<hash-table equal? 0x89ff348> doesn't have an entry for key "c"
Stack Trace:

Ruby は Key が存在しなくても例外は発生せず、nil を返します。

irb(main):003:0> {1=>"a","1"=>"b"}[123]
=> nil

Ruby は Key が存在しなくても例外は発生せず、nil を返します。Python は「まつもとゆきひろ コードの世界」には例外を発生させるとあったような。