From: ts Date: 2003-05-01T00:46:13+09:00 Subject: Re: Arrays >>>>> "O" == Orion Hunter writes: irb> a = Array.new( 4, Array.new ) Well, with 1.8 you'll able to write pigeon% ruby -e 'a = Array.new(4) { [] }; a[0][0] = 1; p a' [[1], [], [], []] pigeon% O> My next approach was to create the array, with a dafault length, and iterate O> over it assigning a new array to each element: there is a problem : you assign a new array to the variable `i' and not to each element O> my_arr = Array.new( 4 ) O> my_arr.each{ |i| at this step the variable `i' reference an element of the array O> i = Array.new but here `i' will make reference to a new Array O> } This is like if you write : i = my_arr[0] i = [] for the first line , `i' make reference to the object `nil' (my_arr[0]) but in the second line, it will make reference to the object `[]' and my_arr[0] is not modified but you can write pigeon% cat b.rb ##!/usr/bin/ruby my_arr = (1..4).collect { [] } my_arr.each {|i| i << 1} p my_arr pigeon% pigeon% b.rb [[1], [1], [1], [1]] pigeon% `<<' will modify the current element Guy Decoux