我有 2 个哈希值,我需要使用 Ruby 将它们组合在值上
hoh = Hash.new
hoh = {"bob::params::search_vol_grp"=>{2=>{"search_group_id"=>2, "vol_ids"=>"2,627,628", "num_shards"=>32, "num_replicas"=>1}, 3=>{"search_group_id"=>3, "vol_ids"=>"3,629,630", "num_shards"=>32, "num_replicas"=>1}, 4=>{"search_group_id"=>4, "vol_ids"=>"4,631,632", "num_shards"=>32, "num_replicas"=>1}}
hob = Hash.new
hob = {"bob::params::search_q_broker"=>{2=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616"}, 3=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616"}, 4=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hsq01p2", "slave_port"=>"61616"}}
我想结束的是以下内容 -
hon = Hash.new
hon = {"bob::params::search"=>{2=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616", "search_group_id"=>2, "vol_ids"=>"2,627,628", "num_shards"=>32, "num_replicas"=>1}, 3=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616", "search_group_id"=>3, "vol_ids"=>"3,629,630", "num_shards"=>32, "num_replicas"=>1}, 4=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616", "search_group_id"=>4, "vol_ids"=>"4,631,632", "num_shards"=>32, "num_replicas"=>1}}
我已经尝试过使用合并和合并的各种尝试!但没有一个产生我正在寻找的最终结果。
有小费吗?
编辑 使用拉里的建议,我能够通过以下方式实现我想要的
hon = Hash.new
hon['bob::params::search'] = Hash.new
hoh.each do |key,value|
value.each do |k, v|
hon['bob::params::search'][k] = hoh['bob::params::search_vol_grp'][k].merge! (hob['bob::params::search_q_broker'][k])
end
end
我还更新了第二个哈希以使用固定数字,而不是 Darshan 指示的字符串。
感谢所有的指点家伙!