I’m trying to insert a hash into an array, following this example: How to make dynamic multi-dimensional array in ruby?. What went wrong?
@array = Array.new
test1 = {"key1" => "value1"}
test2 = {"key2" => "value2"}
test3 = {"key3" => "value3"}
@array.push(0)
@array[0] << test1
# ERROR: can't convert Hash into Integer
@array[0] << test2
@array.push(1)
@array[1] << test2
@array[1] << test3
<<appends to the array, the same aspush, so just do:Or, if you want to overwrite a particular element, say
0:Or do you actually want a two-dimensional array, such that
@array[0][0]["key1"] == "value1"? In that case, you need to insert empty arrays into the right place before you try to append to them: