From: Robert Dober Date: 2007-03-17T07:18:53+09:00 Subject: Re: Issues using array.delete within a loop of the same array On 3/16/07, Fedor Labounko wrote: > 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. Let us see VALUE rb_ary_each(ary) VALUE ary; { long i; for (i=0; ilen; i++) { rb_yield(RARRAY(ary)->ptr[i]); } return ary; } your speculation was correct :) fortuntely i < RARRAY(ary)-> len is reavaluated after each yield! Cheers Robert > -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw