From: Roger Bannister Date: 2004-04-27T09:27:03+09:00 Subject: Re: The Strangeness of Arrays Hal Fulton wrote: > Roger Bannister wrote: > >> Hi >> >> The behaviour below has me completely confuzzled. Why on earth does >> my array suddenly have all rows equal to the last row? >> >> Can anyone tell me if this is expected; or if I have to start wearing >> my Stupid Hat again. > > > It's the strangeness of objects, not arrays. See below... > >> Thanks >> R >> >> irb(main):014:0> @t = Array.new(5, Array.new(4)) > > > This fills @t with five references to the same four-item array. > > If you change one, you change them all. > > I think in recent Ruby you can do this: > > @t = Array.new(5) { Array.new(4) } > > and you'll get different sub-arrays. Try it... > > > Hal > > > > Right, it's the hat for me! I'd completely forgotten the basic example of a = 1; b = a. Thanks guys.