我的哈希是:
hash = { text:'abc', text1:'123', other values }
我希望:
{ text: 'abc123', other values }
如何将两个键合并为一个键(仅合并text+text1
)?
答案 0 :(得分:6)
hash[:text] << hash.delete(:text1)
答案 1 :(得分:5)
您可以使用Object#tap
作为目标:
hash.tap { |h| h[:text] += h.delete(:text1) }
=> {:text=>"abc123", other_values}