That doesn't work under ruby-1.6.8:
arr = Array.new(10) { {} }
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
Some care is required. You can't do:
arr = Array.new(10, {} )
=> [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
because then you have got ten references to the same hash:
arr.collect{|i| i.id}.join(',')
=> "67787102,67787102,67787102,67787102,67787102,67787102,67787102,67787102,67787102,67787102"
f
You need something like:
arr =
10.times { arr << {} }
Regards,
Brian.
···
On Thu, Feb 13, 2003 at 06:02:50AM +0900, Chris Pine wrote:
Another way:
# Create an empty hash in each slot of the array.
arr = Array.new (10) { {} }
# same as:
# arr = Array.new (10) do
# Hash.new
# end