J
james.d.masters
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...
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...