ruby 使用整数作为哈希键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7694317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of integers as hash keys
提问by timpone
Is it appropriate to use integers as keys in a Ruby hash?
在 Ruby 哈希中使用整数作为键是否合适?
Every example from documentation shows a string or symbol being used as a key, but never an integer.
文档中的每个示例都显示了用作键的字符串或符号,但绝不是整数。
Internally, would integers somehow get converted to strings? I have seen some conflicting information on the subject.
在内部,整数会以某种方式转换为字符串吗?我在这个主题上看到了一些相互矛盾的信息。
In other words, is there any significant disadvantage to using integer keys to a hash?
换句话说,对散列使用整数键有什么明显的缺点吗?
回答by Tilo
of course you can use integers as keys...
当然你可以使用整数作为键......
h = {1 => 'one', 2 => 'two', 3 => 'three'}
(1..3).each do |i|
puts h[i]
end
=>
=>
one
two
there
irb is your friend! try it..
irb 是你的朋友!尝试一下..
In fact you can use any Ruby objectas the key (or the value). We usually don't think about using Hashes like this, but it could be quite useful.
事实上,您可以使用任何 Ruby 对象作为键(或值)。我们通常不会考虑像这样使用哈希,但它可能非常有用。
回答by dylankb
Others looking at the answers here might find it interesting to know that an exception happens when you use integers as symbol keys in a Ruby hash{symbol: value}
其他查看此处答案的人可能会发现,当您在Ruby 散列中使用整数作为符号键时会发生异常,这可能会很有趣{symbol: value}
hash = {1: 'one'} # will not work
hash = {1 => 'one'} # will work
Requested Explanation:
要求的解释:
The simplest answer for why the first example fails is probably that to_symis not a method that's been implemented for Fixnumintegers.
第一个示例失败的最简单答案可能是这to_sym不是针对Fixnum整数实现的方法。
To go more in depth to maybe explaining why that is, one of the main benefits to using symbols is that two symbols are in fact "the same object". Or at least they share the same object ids.
为了更深入地解释为什么会这样,使用符号的主要好处之一是两个符号实际上是“同一个对象”。或者至少它们共享相同的对象 ID。
:foo.object_id == :foo.object_id
=> true
Strings that are the same do not share the same objects, and therefore do not share the same object ids.
相同的字符串不共享相同的对象,因此不共享相同的对象 ID。
"foo".object_id == "foo".object_id
=> false
Like symbols, Fixnum integers that are the same will have the same object ids. Therefore you don't really need to convert them into symbols.
与符号一样,相同的 Fixnum 整数将具有相同的对象 ID。因此,您实际上并不需要将它们转换为符号。
one = 1
=> 1
uno = 1
=> 1
one.object_id
=> 3
one.object_id == uno.object_id
=> true
回答by óscar López
The only requirement for using an object as a hash key is that it must respond to the message hash with a hash value, and the hash value for a given key must not change. For instance, if you call this:
使用对象作为散列键的唯一要求是它必须用散列值响应消息散列,并且给定键的散列值不能改变。例如,如果你调用它:
1.hash()
You can see that the number 1 indeed responds to the hash message
可以看到数字 1 确实响应了哈希消息
回答by knut
There are already answers about the is it possible?.
已经有关于is it possible?.
An explanation, why there are no examples with integers as Hash-keys.
一个解释,为什么没有整数作为哈希键的例子。
Hash-keys have (most of the times) a meaning. It may be an attribute name and its value (e.g. :color => 'red'...).
哈希键(大多数情况下)具有含义。它可能是一个属性名称及其值(例如:color => 'red'...)。
When you have an integer as a key, your semantic may be 'first, second ...' (1). But then you don't use a hash, but an array to store your values.
当你有一个整数作为键时,你的语义可能是“第一,第二......”(1)。但是,您不使用散列,而是使用数组来存储您的值。
(1) A counterexample may be a foreign key in a database.
(1) 反例可能是数据库中的外键。

