From: Robert Klemme Date: 2004-11-11T21:53:28+09:00 Subject: Re: array.each restart when array is changed "Kevin B�rgens" schrieb im Newsbeitrag news:2vfjurF2ljhdfU1@uni-berlin.de... > Hi! > > > How to restart a "each" iteration when the iterated array is changed? I do > it like this > > changed=false > while (changed) > changed=false > array.each {|e| > if (today_is_christmas) > array=array+=["Hello santa claus"] This is sufficient: array += +=["Hello santa claus"] > changed=true > end > } > > Is there a more elegant way to do this? I would not change the array in place while iterating. Without more knowledge about your scenario I'd do this: added = [] begin added.clear array.each do |e| if (today_is_christmas) added << "Hello santa claus" end end array.concat added end until added.empty? If you want to ensure, that each element is traversed only once, a queue approach is better: queue = array.dup until queue.empty? e = queue.shift if today_is_christmas(e) x = "Hello santa claus" queue << x array << x end end This is much more performant because with the other solution you end up running through the same array members over and over again. Are you doing some kind of BFS? Kind regards robert