合并哈希键

时间:2016-06-21 11:30:59

标签: ruby hashmap

我的哈希是:

hash = { text:'abc', text1:'123', other values }

我希望:

{ text: 'abc123', other values }

如何将两个键合并为一个键(仅合并text+text1)?

2 个答案:

答案 0 :(得分:6)

hash[:text] << hash.delete(:text1)

答案 1 :(得分:5)

您可以使用Object#tap作为目标:

hash.tap { |h| h[:text] += h.delete(:text1) }
=> {:text=>"abc123", other_values}