From: Joel VanderWerf Date: 2005-06-18T08:57:27+09:00 Subject: Re: Why do arrays work this way? Nigel Wilkinson wrote: > Hi folks > > I'm using ruby 1.8.2 as comes with Mandriva 2005LE and get the following > behaviour with arrays which doesn't seem logical to me. > > In irb I do > > irb(main):058:0* a=[1,2,3,4,5] > => [1, 2, 3, 4, 5] > > straight forward enough > > irb(main):059:0> b=a > => [1, 2, 3, 4, 5] > irb(main):060:0> c=a > => [1, 2, 3, 4, 5] > > I've set 2 more variables to equal 'a' > > irb(main):061:0> b[3] = 'w' > => "w" > > I believe this changes index 3 in'b' to now be equal to 'w' > > do > irb(main):062:0> b > => [1, 2, 3, "w", 5] > > that's exactly as I expected, BUT > > irb(main):063:0> a > => [1, 2, 3, "w", 5] > irb(main):064:0> c > => [1, 2, 3, "w", 5] > > so what's the logic behind 'a' & 'c' changing when I change 'b'. It's the same array in each case. The 3 variables each refer to it.