From: MenTaLguY Date: 2007-03-17T06:02:36+09:00 Subject: Re: Issues using array.delete within a loop of the same array On Sat, 17 Mar 2007 05:45:05 +0900, james.d.masters@gmail.com wrote: > Yes, I know that there are other ways to empty an array (i.e. a = > []). a = [] creates a new, empty array; it does not empty the existing array. > My code is more complex and I'm more interested in why this is > happening It's disallowed by design -- removing elements in the midst of iteration would be a bit like trying to remove carpet tiles while you are standing on them. > and if there is a work-around for removing array items while > iterating over the same array. Have you considered using Array#reject! to iterate over the array? You can use the result of the block for each iteration (true or false) to control whether each element is dropped or retained once the iteration completes. a.reject! { |e| # ... do stuff e > 3 # discard elements greater than three } -mental