hash_volumes
以下のハッシュに、 hash のキーとinstance_id
一致するキーがあるかどうかを確認しています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`
},
}
上記で説明したように、これら 2 つのハッシュをマージできません。私の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.