0
我试着通过#Hash.new添加散列没有成功,现在我正在尝试.merge按照有限的成功论坛。我正在尝试将#rand(1..100)添加到[0]中,而无需手动进入哈希。有任何想法吗?添加散列到散列(红宝石)
#age = Hash.new
#email = Hash.new
#age2 = rand(1..100)
people = [
{
"first_name" => "Bob",
"last_name" => "Jones",
"hobbies" => ["basketball", "chess", "phone tag"]
},
{
"first_name" => "Molly",
"last_name" => "Barker",
"hobbies" => ["programming", "reading", "jogging"]
},
{
"first_name" => "Kelly",
"last_name" => "Miller",
"hobbies" => ["cricket", "baking", "stamp collecting"]
}
]
people[0].each do |w|
people.merge({:age => rand(1..100)})
puts "array 0 is #{w}"
end
puts p people
你的问题很混乱。你能提供一个你想要的输入和输出的例子吗? – Shelvacu
为每个属性分散散列通常是不好的设计。另外,除非需要指定默认值,如'Hash.new(0)',否则使用'{}'而不是'Hash.new'。 – tadman
输出 - 人= [{ “first_name的”=> “鲍勃”, “姓氏”=> “琼斯”, “年龄”=>兰特(1..100), “爱好”=> [“篮球”,“国际象棋”,“电话标签”] }, – whatabout11