From: Matthew Smillie Date: 2006-07-03T18:36:11+09:00 Subject: Re: Help please On Jul 2, 2006, at 17:07, Dark Ambient wrote: > I'm responding on my post above (re: why pic_names.delete(name), jumps > an element in the loop. > There must be a reason for this and still would like to some > elucidation on the matter. > > However I found by creating a new array pix2bmoved =[] > and doing a pix2bmoved.push[name], things seem to work right. You're one step away from answering your own question: you're modifying the same array that you're iterating over, which can make things go slightly crazy. Try this example, for instance: a = (1..10).to_a a.each { |x| puts x a.delete(x) } This only prints 1, 3, 5, 7, 9. The technical reason for this is that (if I recall correctly) Ruby is using an internal array index to keep track of which element of a it's on. When you delete a[0] (which is 1), everything shifts down in the array. The correct thing to do would be to use a[0] again (which is now 2), but Ruby instead uses a[1] (which is now 3). Then a[1] (3) gets deleted, something new moves to a[1] (4), and Ruby skips that to do a[2] (5), and so on. The general explanation is that modifying the array (or hash, or whatever) that you're enumerating over can cause unpredictable behaviour. This is true in many languages, not just Ruby, because detecting and correcting for modifications during such an iteration is a difficult problem in general. The safest thing to do (as you've discovered) is to make modifications to a separate array. matthew smillie.