From: Robert Klemme Date: 2005-09-08T04:56:29+09:00 Subject: Re: weird result when updating an array element 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 > def initialize > @ver = Array.new(2, {'pos' => Array.new(3), 'col' => > Array.new(4)}) @ver[0]['pos'][0] = 0 > puts @ver.inspect > 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 => # >> obj.object_id => 135042152 >> a=Array.new(3,obj) => [#, #, #] >> 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 } => [#, #, #] >> 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 robert