Ruby Hash Manupopulation - acv-tamhc/code-style GitHub Wiki

Deep merge

deep_merge(other_hash, &block) public

h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}

h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }

using with &block

h1 = { a: 100, b: 200, c: { c1: 100 } }
h2 = { b: 250, c: { c1: 200 } }
h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
# => { a: 100, b: 450, c: { c1: 300 } }

more reference link

Merge two array => hash

symbols = %i(one two three)
numbers = 4..6
numbers.zip(symbols) # => [4, :one], [5, :two], [6, :three](/acv-tamhc/code-style/wiki/4,-:one],-[5,-:two],-[6,-:three)
numbers.zip(symbols).flattern # => [4, :one, 5, :two, 6, :three]