From: Robert Klemme Date: 2010-04-23T23:37:16+09:00 Subject: Re: What is wrong with this few lines of code 2010/4/23 Rolf Pedersen : > The code ar = Array.new(n, []) actually makes n copies of the same object > (empty array), so each time you do either ar[0] << e or ar[1] << e you are > appending elements to the same array. That description is inconsistent and might confuse one or the other reader: there are no copies made of the *object* (Array in this case) but only of the *reference*! irb(main):001:0> a = Array.new(3,[]) => [[], [], []] irb(main):002:0> a.map {|x| x.object_id} => [135000124, 135000124, 135000124] irb(main):003:0> a.map {|x| x.object_id}.uniq => [135000124] The idiom you describe when you say "copy" is the block form of Arra.new: irb(main):004:0> a = Array.new(3) { [] } => [[], [], []] irb(main):005:0> a.map {|x| x.object_id} => [135669212, 135669198, 135669184] irb(main):006:0> a.map {|x| x.object_id}.uniq => [135669212, 135669198, 135669184] > What I guess you want is to different objects, and you can do that by e.g. > ar = Array.new(2){[]} > (or just ar = [[],[]]   ...or many other ways :o) ) Exactly. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/