我正在检查下面的hash hash_volumes是否有一个其instance_id与hash hash_instance的键匹配的键。
hash_volumes = { :"vol-d16d12b8" => { :instance_id => "i-4e4ba679", }, } hash_instance = { :"i-4e4ba679" => { :arch => "x86_64", }, }如果是,那么我需要将它合并到hash_instance 。 我发现vol-d16d12b8与实例i-4e4ba679 ,因此我想将它与hash_instance合并,以便最终的hash_instance如下所示:
hash_instance = { :"i-4e4ba679" => { :arch => "x86_64", :volume => "vol-d16d12b8" # this is new entry to `hash_instance` }, }如上所述,我无法合并这两个哈希值。 我怀疑我的if语句是错误的。 请看下面的代码:
hash_volumes.each_key do |x| hash_instance.each_key do |y| if hash_volumes[x][:instance_id] == y ## I think this line is the problem hash_instance[y][:volume] = x end end end hash_instance输出:
{ :"i-4e4ba679" => { :arch => "x86_64" } }上面的代码给出了hash_instance而没有向它添加volume 。 我尝试如下,但没有一个工作:
if hash_volumes[x][:instance_id] == "#{y}" # => this if statement gives me syntax error.....
if hash_volumes[x][:instance_id] =~ /"#{y}"/ # => this if statement does not make any changes to above output.I am checking whether the hash hash_volumes below has a key whose instance_id matches with a key of hash hash_instance.
hash_volumes = { :"vol-d16d12b8" => { :instance_id => "i-4e4ba679", }, } hash_instance = { :"i-4e4ba679" => { :arch => "x86_64", }, }If it does, then I need to merge it to the hash_instance. I find that vol-d16d12b8 matches with the instance i-4e4ba679 and hence I want to merge it with hash_instance so that the final hash_instance will look like below:
hash_instance = { :"i-4e4ba679" => { :arch => "x86_64", :volume => "vol-d16d12b8" # this is new entry to `hash_instance` }, }I am not able to merge these two hashes as explained above. I suspect my if statement is wrong. Please take a look at my code below:
hash_volumes.each_key do |x| hash_instance.each_key do |y| if hash_volumes[x][:instance_id] == y ## I think this line is the problem hash_instance[y][:volume] = x end end end hash_instanceOutput:
{ :"i-4e4ba679" => { :arch => "x86_64" } }The code above gives hash_instance without adding volume to it. I tried as below, but none worked:
if hash_volumes[x][:instance_id] == "#{y}" # => this if statement gives me syntax error.....
if hash_volumes[x][:instance_id] =~ /"#{y}"/ # => this if statement does not make any changes to above output.最满意答案
hash_volumes = { :"vol-d16d12b8" => { :instance_id => "i-4e4ba679", }, } hash_instance = { :"i-4e4ba679" => { :arch => "x86_64", }, } hash_volumes.each do |key, val| id = val[:instance_id] #returns nil if the there is no :instance_id key if id id_as_sym = id.to_sym if hash_instance.has_key? id_as_sym hash_instance[id_as_sym][:volume] = id end end end --output:-- {:"i-4e4ba679"=>{:arch=>"x86_64", :volume=>"i-4e4ba679"}} hash_volumes = { :"vol-d16d12b8" => { :instance_id => "i-4e4ba679", }, } hash_instance = { :"i-4e4ba679" => { :arch => "x86_64", }, } hash_volumes.each do |key, val| id = val[:instance_id] #returns nil if the there is no :instance_id key if id id_as_sym = id.to_sym if hash_instance.has_key? id_as_sym hash_instance[id_as_sym][:volume] = id end end end --output:-- {:"i-4e4ba679"=>{:arch=>"x86_64", :volume=>"i-4e4ba679"}}更多推荐
发布评论