From: Fedor Labounko Date: 2007-03-17T06:29:47+09:00 Subject: Re: Issues using array.delete within a loop of the same array ------=_Part_71613_20059429.1174080584362 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Now this is pure speculation on my part and I'm not offering a solution to the problem, rather a likely explanation. The other solutions all look good to me as alternatives. It would make sense that what Array#each is doing behind the scenes is simply using an index and a for loop up to the size of the array, and then passing you a[i]. So then during the first step you delete a[0], which renumbers the elements in the array. During the second step you delete a[1], which is now actually 3, since that's now the second element in the array. Continue in this manner you'll skip every other element in the array. On 3/16/07, james.d.masters@gmail.com wrote: > > This is a simplified example for what I'm trying to do but gets the > point across: > > irb(main):001:0> a = [1,2,3,4,5] > => [1, 2, 3, 4, 5] > irb(main):002:0> a.each {|e| a.delete e} > => [2, 4] > > I would expect the array to be emptied. Insight please... > > Yes, I know that there are other ways to empty an array (i.e. a = > []). My code is more complex and I'm more interested in why this is > happening and if there is a work-around for removing array items while > iterating over the same array. There will be other cases in this code > where I will only want to remove some of the items. > > Thanks in advance... > > > ------=_Part_71613_20059429.1174080584362--