On Apr 19, 2006, at 11:03 AM, Erard Sebastien wrote:
I can’t understand why this piece of code doesn’t return the array
expected ?
my_array = Array.new(3, Array(3))
my_array = Array.new(3) { Array.new }
[[2, 3, 4] [2, 3, 4] [2, 3, 4]]
Is my code wrong ?
The two argument form of the constructor uses the same object in each
slot. Thus, if you change one, all are modified. The block form I
used above will be called to generate each object, getting you the
desired results.
Hope that helps.
James Edward G. II
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.