I have an array in Ruby consisting of 5 empty arrays. I am trying to use the <<
operator to insert a string into the first array, but the result is that the string falls into ALL arrays. Please help me figure this out.
Expected Result:
# => [["car"], [], [], [], []]
but instead I get:
# => [["car"], ["car"], ["car"], ["car"], ["car"]]
irb dump:
1.9.3-p194 :001 > output = Array.new(5, []) => [[], [], [], [], []] 1.9.3-p194 :002 > output.inspect => "[[], [], [], [], []]" 1.9.3-p194 :003 > output[0].inspect => "[]" 1.9.3-p194 :004 > output[0] << "car" => ["car"] 1.9.3-p194 :005 > output.inspect => "[[\"car\"], [\"car\"], [\"car\"], [\"car\"], [\"car\"]]"
source share