# Weird result when updating an array element

**URL:** <https://rubytalk.org/t/weird-result-when-updating-an-array-element/20670>\
**Category:** ruby-talk\
**Created:** [7 September 2005 19:38 UTC](https://rubytalk.org/t/weird-result-when-updating-an-array-element/20670 "2005-09-07T19:38:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nelson\_Owen](https://avatars.discourse-cdn.com/v4/letter/n/958977/32.png) [@Nelson\_Owen](https://rubytalk.org/u/Nelson_Owen)\
**Post date:** [7 September 2005 19:38 UTC](https://rubytalk.org/t/weird-result-when-updating-an-array-element/20670/1 "2005-09-07T19:38:49Z")

</div>

Given the code below, only one element in @ver should be changed, however when I execute the code all the elements are changed. Can anyone explain why this is happening?

class SomeClass  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ver = Array.new(2, {'pos' =\> Array.new(3), 'col' =\> Array.new(4)})  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ver[0]['pos'][0] = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @ver.inspect  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end  
SomeClass.new

actual result:  
[{"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}, {"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}]

expected result:  
[{"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}, {"col"=\>[nil, nil, nil, nil], "pos"=\>[nil, nil, nil]}]

---

<div class="post-metadata">

**Author:** ![Simon\_Kroger](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Simon\_Kroger](https://rubytalk.org/u/Simon_Kroger)\
**Post date:** [7 September 2005 19:54 UTC](https://rubytalk.org/t/weird-result-when-updating-an-array-element/20670/2 "2005-09-07T19:54:28Z")

</div>

Nelson Owen wrote:

> Given the code below, only one element in @ver should be changed, however when I execute the code all the elements are changed. Can anyone explain why this is happening?
> 
> class SomeClass  
> &nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ver = Array.new(2, {'pos' =\> Array.new(3), 'col' =\> Array.new(4)})  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ver[0]['pos'][0] = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @ver.inspect  
> &nbsp;&nbsp;&nbsp;end  
> end  
> SomeClass.new
> 
> actual result:  
> [{"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}, {"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}]
> 
> expected result:  
> [{"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}, {"col"=\>[nil, nil, nil, nil], "pos"=\>[nil, nil, nil]}]

from the Array docs:

...it is created with size copies of obj (that is, size references to the same obj).

So ver[0] and ver[1] are in fact pointing to the same object.

You can use:

@ver = Array.new(2) {{'pos' =\> Array.new(3), 'col' =\> Array.new(4)}}

wich looks odd, but works 🙂

cheers

Simon

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [7 September 2005 19:56 UTC](https://rubytalk.org/t/weird-result-when-updating-an-array-element/20670/3 "2005-09-07T19:56:29Z")

</div>

> Given the code below, only one element in @ver should be changed,  
> however when I execute the code all the elements are changed. Can  
> anyone explain why this is happening?
> 
> class SomeClass  
> &nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ver = Array.new(2, {'pos' =\> Array.new(3), 'col' =\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(4)}) @ver[0]['pos'][0] = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @ver.inspect  
> &nbsp;&nbsp;&nbsp;end  
> end  
> SomeClass.new
> 
> actual result:  
> [{"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}, {"col"=\>[nil,  
> nil, nil, nil], "pos"=\>[0, nil, nil]}]
> 
> expected result:  
> [{"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}, {"col"=\>[nil,  
> nil, nil, nil], "pos"=\>[nil, nil, nil]}]

You are creating a two element array where the same hash is referenced for each array element:

> > obj = Object.new

=\> #\<Object:0x101928d0\>

> > obj.object\_id

=\> 135042152

> > a=Array.new(3,obj)

=\> [#\<Object:0x101928d0\>, #\<Object:0x101928d0\>, #\<Object:0x101928d0\>]

> > a.map {|o| o.object\_id}

=\> [135042152, 135042152, 135042152]

> > a.map {|o| o.object\_id}.uniq

=\> [135042152]

You want the block form of array init:

> > a = Array.new(3) { Object.new }

=\> [#\<Object:0x1017a528\>, #\<Object:0x1017a480\>, #\<Object:0x1017a3f0\>]

> > a.map {|o| o.object\_id}

=\> [134992532, 134992448, 134992376]

> > a.map {|o| o.object\_id}.uniq

=\> [134992532, 134992448, 134992376]

In your case

@ver = Array.new(2){ {'pos' =\> Array.new(3), 'col' =\> Array.new(4)} }

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Nelson Owen \<nowen@i-55.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Nelson\_Owen](https://avatars.discourse-cdn.com/v4/letter/n/958977/32.png) [@Nelson\_Owen](https://rubytalk.org/u/Nelson_Owen)\
**Post date:** [7 September 2005 20:17 UTC](https://rubytalk.org/t/weird-result-when-updating-an-array-element/20670/4 "2005-09-07T20:17:55Z")

</div>

Simon Kröger wrote:

> Nelson Owen wrote:
> 
> > Given the code below, only one element in @ver should be changed, however when I execute the code all the elements are changed. Can anyone explain why this is happening?
> > 
> > class SomeClass  
> > &nbsp;&nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ver = Array.new(2, {'pos' =\> Array.new(3), 'col' =\> Array.new(4)})  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ver[0]['pos'][0] = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @ver.inspect  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > SomeClass.new
> > 
> > actual result:  
> > [{"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}, {"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}]
> > 
> > expected result:  
> > [{"col"=\>[nil, nil, nil, nil], "pos"=\>[0, nil, nil]}, {"col"=\>[nil, nil, nil, nil], "pos"=\>[nil, nil, nil]}]
> 
> from the Array docs:
> 
> ...it is created with size copies of obj (that is, size references to the same obj).
> 
> So ver[0] and ver[1] are in fact pointing to the same object.
> 
> You can use:
> 
> @ver = Array.new(2) {{'pos' =\> Array.new(3), 'col' =\> Array.new(4)}}
> 
> wich looks odd, but works 🙂
> 
> cheers
> 
> Simon
> 
> .

Ah, I see now. Thanks!
