, , , , . , , Array#each_index.
VALUE rb_ary_each_index(VALUE ary)
{
long i;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
for (i=0; i<RARRAY_LEN(ary); i++) {
rb_yield(LONG2NUM(i));
}
return ary;
}
, for 0 . 0 .
When you remove an element from the array, everything after its offset 1. If i = 5you call a.delete_at(i), which means a[6]now a[5]. a[7]now a[6]. Etc. The next iteration will have i = 6, which means that you actually missed the item.
To illustrate and suggest that you want to remove 2.
i = 0
a = [1,2,2,2,3,4,5]
^
-------------------
i = 1
a = [1,2,2,2,3,4,5]
^
a.delete_at(i)
a = [1,2,2,3,4,5]
-------------------
i = 2
a = [1,2,2,3,4,5]
^
a.delete_at(i)
a = [1,2,3,4,5]
-------------------
i = 3
a = [1,2,3,4,5]
^
-------------------
i = 4
a = [1,2,3,4,5]
^
-------------------
i = 5
a = [1,2,3,4,5]
-------------------
i = 6
a = [1,2,3,4,5]
Note that the last two iterations left the array, because the array is now two elements smaller than before.