1. Surprise
Array.new(5, [])
returns a new array with 5 []
elements, but those elements are actually references to the same object:
This is confirmed by looking at the underlying ids:
2. Solution
The correct way of generating a list of distinct object is to pass []
to a block. The block is evaluated for each new element in the array:
3. How it works
[]
was returning a new object on each call: