如何使用Ruby将哈希值中的值附加到另一个哈希值

时间:2017-04-26 14:03:57

标签: arrays ruby hashmap

我有一个哈希:

{:userdata=>["field: email,start: 0,end: 0"]}

我想添加另一个看起来像这样的哈希:

{:userdata=>["field: resource.attributes.account_source.iban,start: 0,end: 0", "field: resource.attributes.account_source.person_name,start: 0,end: 0", "field: resource.attributes.account_target.iban,start: 0,end: 0", "field: resource.attributes.account_target.person_name,start: 0,end: 0"], :iddata=>["field: resource.attributes.account_source.account_id,start: 0,end: 0", "field: resource.attributes.account_source.person_id,start: 0,end: 0", "field: resource.attributes.account_target.account_id,start: 0,end: 0", "field: resource.attributes.account_target.person_id,start: 0,end: 0"], :infodata=>["field: resource.attributes.reason,start: 0,end: 0"], :balancedata=>["field: resource.attributes.amount,start: 0,end: 0"]}

第二个哈希值和数组中有多个键,所以我需要在正确的位置添加值,这应该是这样的:

 {:userdata=>["field: resource.attributes.account_source.iban,start: 0,end: 0", "field: resource.attributes.account_source.person_name,start: 0,end: 0", "field: resource.attributes.account_target.iban,start: 0,end: 0", "field: resource.attributes.account_target.person_name,start: 0,end: 0", "field: email,start: 0,end: 0"], ....

第二个散列更复杂,可能包含我试图"合并"的散列的相同键。但当然缺少值,这就是我想在第二个哈希中添加的内容。

1 个答案:

答案 0 :(得分:0)

您可以将Hash#merge与块一起使用,并使用Array#|来获取两个数组的并集:

h1 = { userdata: ['field: email,start: 0,end: 0'] }
h2 = { userdata: ['field: resource.attributes.account_source.iban,start: 0,end: 0', 'field: resource.attributes.account_source.person_name,start: 0,end: 0', 'field: resource.attributes.account_target.iban,start: 0,end: 0', 'field: resource.attributes.account_target.person_name,start: 0,end: 0'], iddata: ['field: resource.attributes.account_source.account_id,start: 0,end: 0', 'field: resource.attributes.account_source.person_id,start: 0,end: 0', 'field: resource.attributes.account_target.account_id,start: 0,end: 0', 'field: resource.attributes.account_target.person_id,start: 0,end: 0'], infodata: ['field: resource.attributes.reason,start: 0,end: 0'], balancedata: ['field: resource.attributes.amount,start: 0,end: 0'] }

h3 = h1.merge(h2) { |_, old_array, new_array| new_array | old_array | }
# {:userdata=>["field: resource.attributes.account_source.iban,start: 0,end: 0", "field: resource.attributes.account_source.person_name,start: 0,end: 0", "field: resource.attributes.account_target.iban,start: 0,end: 0", "field: resource.attributes.account_target.person_name,start: 0,end: 0", "field: email,start: 0,end: 0"], :iddata=>["field: resource.attributes.account_source.account_id,start: 0,end: 0", "field: resource.attributes.account_source.person_id,start: 0,end: 0", "field: resource.attributes.account_target.account_id,start: 0,end: 0", "field: resource.attributes.account_target.person_id,start: 0,end: 0"], :infodata=>["field: resource.attributes.reason,start: 0,end: 0"], :balancedata=>["field: resource.attributes.amount,start: 0,end: 0"]}

请注意,:userdata'userdata'不是相同的Ruby对象,并且不被认为是哈希中的相同键。

最后,哈希值中的值不是哈希值,而是字符串数组。具有相同字段但不同值的两个字符串将被视为完全不同:

['field: a'] | ['field: a, value: 1']
# ["field: a", "field: a, value: 1"]

如果您想要哈希的功能,请使用Hash