Ruby散列中忽略重复键的哪个值会被忽略? [英] Which value for a duplicate key is ignored in a Ruby hash?

查看:180
本文介绍了Ruby散列中忽略重复键的哪个值会被忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个散列有多个指向不同值的相同键的出现次数,那么Ruby如何确定将哪个值分配给该键?

换句话说,换句话说, ,

  hash = {keyone:'value1',keytwo:'value2',keyone:'value3'} 

结果

 警告:第1行的重复键被忽略::keyone 

但我如何知道分配了哪个值至:keyone

解决方案

最后一个覆盖以前的值。在这种情况下,value3成为:keyone 的值。这与 merge 一样。当合并具有相同键的两个散列时,后面的散列(不是接收者,而是参数)中的值会覆盖另一个值。


If a hash has more than one occurrences of identical keys pointing to different values, then how does Ruby determine which value is assigned to that key?

In other words,

hash = {keyone: 'value1', keytwo: 'value2', keyone: 'value3'}

results in

warning: duplicated key at line 1 ignored: :keyone

but how do I know which value is assigned to :keyone?

解决方案

The last one overwrites the previous values. In this case, "value3" becomes the value for :keyone. This works just as the same with merge. When you merge two hashes that have the same keys, the value in the latter hash (not the receiver but the argument) overwrites the other value.

这篇关于Ruby散列中忽略重复键的哪个值会被忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆