# Changing array values

**URL:** <https://rubytalk.org/t/changing-array-values/52825>\
**Category:** ruby-talk\
**Created:** [13 April 2009 20:42 UTC](https://rubytalk.org/t/changing-array-values/52825 "2009-04-13T20:42:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Salveson](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Andrew\_Salveson](https://rubytalk.org/u/Andrew_Salveson)\
**Post date:** [13 April 2009 20:42 UTC](https://rubytalk.org/t/changing-array-values/52825/1 "2009-04-13T20:42:07Z")

</div>

What I'd like is an array of objects, each of which contains an array of  
integers, and each successive object in the array should contain a  
slightly different array of integers than the previous object.

For some reason, when I run the code below:

class TEST  
&nbsp;&nbsp;def initialize(args)  
&nbsp;&nbsp;&nbsp;&nbsp;@args = args  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def describe  
&nbsp;&nbsp;&nbsp;&nbsp;return @args.join(", ")  
&nbsp;&nbsp;end  
end

def test  
&nbsp;&nbsp;old = [0,0,0,0,0,0]  
&nbsp;&nbsp;tests = []  
&nbsp;&nbsp;5.times do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;new = old  
&nbsp;&nbsp;&nbsp;&nbsp;new[rand(old.size)] = rand(5) # randomly changes one of the members  
of the array to a number from 0 to 4  
&nbsp;&nbsp;&nbsp;&nbsp;tests[i] = TEST.new(new)  
&nbsp;&nbsp;&nbsp;&nbsp;puts tests[i].describe  
&nbsp;&nbsp;end  
&nbsp;&nbsp;return tests  
end

I get this output:

0, 0, 0, 0, 0, 0  
0, 0, 0, 0, 0, 1  
0, 0, 0, 0, 0, 4  
0, 0, 0, 2, 0, 4  
0, 0, 1, 2, 0, 4  
[#\<TEST:0x3f3d9f0 @args=[0, 0, 1, 2, 0, 4]\>,  
#\<TEST:0x3f3d9d8 @args=[0, 0, 1, 2, 0, 4]\>,  
#\<TEST:0x3f3d900 @args=[0, 0, 1, 2, 0, 4]\>,  
#\<TEST:0x3f3d828 @args=[0, 0, 1, 2, 0, 4]\>,  
#\<TEST:0x3f3d750 @args=[0, 0, 1, 2, 0, 4]\>]

As you can see, each object in the object array contains the last array  
of integers, even though the objects are being initialized with  
different arrays of integers.

Strangely enough, if I use a single integer everywhere I here use an  
array, everything works just fine.

I cannot for the life of me figure out why this is happening and would  
be extremely grateful it if someone could enlighten me . . .

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Coglan2](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James\_Coglan2](https://rubytalk.org/u/James_Coglan2)\
**Post date:** [13 April 2009 20:46 UTC](https://rubytalk.org/t/changing-array-values/52825/2 "2009-04-13T20:46:00Z")

</div>

> For some reason, when I run the code below:
> 
> class TEST  
> def initialize(args)  
> &nbsp;&nbsp;&nbsp;@args = args  
> end  
> def describe  
> &nbsp;&nbsp;&nbsp;return @args.join(", ")  
> end  
> end
> 
> def test  
> old = [0,0,0,0,0,0]  
> tests =   
> 5.times do |i|  
> &nbsp;&nbsp;&nbsp;new = old  
> &nbsp;&nbsp;&nbsp;new[rand(old.size)] = rand(5) # randomly changes one of the members  
> of the array to a number from 0 to 4  
> &nbsp;&nbsp;&nbsp;tests[i] = TEST.new(new)  
> &nbsp;&nbsp;&nbsp;puts tests[i].describe  
> end  
> return tests  
> end
> 
> I get this output:
> 
> 0, 0, 0, 0, 0, 0  
> 0, 0, 0, 0, 0, 1  
> 0, 0, 0, 0, 0, 4  
> 0, 0, 0, 2, 0, 4  
> 0, 0, 1, 2, 0, 4  
> [#\<TEST:0x3f3d9f0 @args=[0, 0, 1, 2, 0, 4]\>,  
> #\<TEST:0x3f3d9d8 @args=[0, 0, 1, 2, 0, 4]\>,  
> #\<TEST:0x3f3d900 @args=[0, 0, 1, 2, 0, 4]\>,  
> #\<TEST:0x3f3d828 @args=[0, 0, 1, 2, 0, 4]\>,  
> #\<TEST:0x3f3d750 @args=[0, 0, 1, 2, 0, 4]\>]

When you set 'new = old', you're just creating another reference to the same  
array. To copy the array, use 'new = old.dup'. (dup = duplicate)

---

<div class="post-metadata">

**Author:** ![Andrew\_Salveson](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Andrew\_Salveson](https://rubytalk.org/u/Andrew_Salveson)\
**Post date:** [13 April 2009 20:51 UTC](https://rubytalk.org/t/changing-array-values/52825/3 "2009-04-13T20:51:42Z")

</div>

James Coglan wrote:

> > end  
> > [#\<TEST:0x3f3d9f0 @args=[0, 0, 1, 2, 0, 4]\>,  
> > #\<TEST:0x3f3d9d8 @args=[0, 0, 1, 2, 0, 4]\>,  
> > #\<TEST:0x3f3d900 @args=[0, 0, 1, 2, 0, 4]\>,  
> > #\<TEST:0x3f3d828 @args=[0, 0, 1, 2, 0, 4]\>,  
> > #\<TEST:0x3f3d750 @args=[0, 0, 1, 2, 0, 4]\>]
> 
> When you set 'new = old', you're just creating another reference to the  
> same  
> array. To copy the array, use 'new = old.dup'. (dup = duplicate)

Ahh, fantastic! Thank you.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
