From: Gavin Kistner Date: 2005-04-25T23:22:53+09:00 Subject: Re: Array::new On Apr 25, 2005, at 8:09 AM, Robert Klemme wrote: >> If you come to this code, by looking at the Array documentation where >> the example >> Array.new(2, Hash.new) � [{}, {}] >> is given, I agree that this documentation is misleading for beginners. > > Yep, true. Probably a better example would be > >>> Array.new(2, {"foo"=>"bar"}) > => [{"foo"=>"bar"}, {"foo"=>"bar"}] Though even then, it would appear (to someone wanting the specific functionality of the OP, and probably 'most' cases) that the parameter is used as a template, with .dup used for each instance. I think the example should illustrate that the objects are in fact the same, and discuss the end result each has: my_array = Array.new( 2, { :foo => :bar } ) my_array[ 1 ][ :jimmy ] = :jammy p my_array #=> [{:foo=>:bar, :jimmy=>:jammy}, {:foo=>:bar, :jimmy=>:jammy}] my_array = Array.new( 2 ){ { :foo => :bar } } my_array[ 1 ][ :jimmy ] = :jammy p my_array #=> [{:foo=>:bar}, {:foo=>:bar, :jimmy=>:jammy}]