红宝石哈希与重复键? [英] Ruby Hash with duplicate keys?
本文介绍了红宝石哈希与重复键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能在Ruby中创建一个允许重复键的散列?
Is it possible to create a hash in Ruby that allows duplicate keys?
我在Ruby 1.9.2中工作。
I'm working in Ruby 1.9.2.
推荐答案
这可能会击败哈希的目的,不是吗?
This would kinda defeat the purpose of a hash, wouldn't it?
如果您想要一个键指向多个元素,请将它指向一个数组:
If you want a key to point to multiple elements, make it point to an array:
h = Hash.new { |h,k| h[k] = [] }
h[:foo] << :bar
h #=> {:foo=>[:bar]}
h[:foo] << :baz
h #=> {:foo=>[:bar, :baz]}
这篇关于红宝石哈希与重复键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文