You could do it like this:
array = []
3.times do |i|
array[i] = {'a' => 10}
array[i].merge!({'b' => 20})
array[i].merge!({'c' => 30})
end
This would populate the array from array[0]
to array[2]
.
Pregunta
I want to create an array of hash like this
array[0] = {'a' => 10, 'b' => 20, 'c' => 30 }
array[1] = {'a' => 10, 'b' => 20, 'c' => 30 }
....
And I want to create the array by adding each element one by one like this
array[1] add {'a' => 10}
array[1] add {'b' => 20}
array[1] add {'c' => 30}
array[2] add {'a' => 10}
array[2] add {'b' => 20}
array[2] add {'c' => 30}
How can I do this?
Solución 2
You could do it like this:
array = []
3.times do |i|
array[i] = {'a' => 10}
array[i].merge!({'b' => 20})
array[i].merge!({'c' => 30})
end
This would populate the array from array[0]
to array[2]
.
Otros consejos
Try using merge!
:
array[1] = {}
array[1].merge!('a' => 10)
array[1].merge!('b' => 20)
array[1].merge!('c' => 30)