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