From: Rolf Pedersen Date: 2010-04-23T22:26:58+09:00 Subject: Re: What is wrong with this few lines of code --0016e64419588624890484e761f5 Content-Type: text/plain; charset=ISO-8859-1 Hi Damjan 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. 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) ) Best regards, Rolf On Fri, Apr 23, 2010 at 2:49 PM, Damjan Rems wrote: > > ar = Array.new( 2,[]) > %w(a b).each do |e| > ar[0] << e > ar[1] << e > end > ar.each do |e| > e.each {|line| p line} > end > > Returns > "a" > "a" > "b" > "b" > "a" > "a" > "b" > "b" > > I would expect: > "a" > "b" > "a" > "b" > > by > TheR > -- > Posted via http://www.ruby-forum.com/. > > --0016e64419588624890484e761f5--