我有一个哈希数组。我想为哈希添加一个值。我将变量作为哈希键或值。我的代码是
variable = "Test"
array[0] = {"a" => "10"}
array[i-1].merge({"#{variable}" => "60" })
我将哈希值设为array[0] = [{"a" => "10"}, {"variable" => "60"}]
我想要变量值的哈希,如下面的
array[0] = [{"a" => "10"}, {"Test" => "60"}]
我该怎么做?
答案 0 :(得分:0)
我认为这就是你所追求的。您已经有了哈希,所以只需直接设置密钥即可。
variable = "Test"
array = []
array << {"a" => "10"}
array[0] = [array[0], {variable => "60"}]
根据评论,听起来像是在array[0] => [{"a" => "10"}, {"Test" => "60"}]
之后。似乎很奇怪array[0]
将是值的散列键和的一部分,但这应该产生:
variable = "Test"
array = []
array << {"a" => "10"}
{array[0] => [array[0], {variable => "60"}]}
#=> {{"a"=>"10"}=>[{"a"=>"10"}, {"Test"=>"60"}]}