I think I’ve found a gotcha. Really, it should be expected behavior, but
it wasn’t immediately obvious to me, so I thought it’s at least worth
mentioning.
Let’s say you want to iterate through an array and delete any items in
that array that match a certain criteria. I thought it’d make sense to
do this:
items.each do |x|
if x == “”
items.delete(x)
end
end
If items.each referred to items[] by reference, this would make sense.
But since it references by value, you’re literally changing the array
live, as you’re iterating through it. This means that, if you delete an
item in mid-iteration, you change the index of items[]. Since you
deleted something, you skip the next item.
Do I have this right?
Am I right in assuming that it’s possible to create an infinite loop
this way by continually push()ing things on to items[]?
In message “Re: Array#each Looping Gotcha”
on Thu, 20 Apr 2006 07:43:46 +0900, Nathan O. [email protected] writes:
|items.each do |x|
| if x == “”
| items.delete(x)
| end
|end
|
|
|If items.each referred to items[] by reference, this would make sense.
|But since it references by value, you’re literally changing the array
|live, as you’re iterating through it. This means that, if you delete an
|item in mid-iteration, you change the index of items[]. Since you
|deleted something, you skip the next item.
|
|1) Do I have this right?
I’m not sure what you mean by the word “right” here. It’s ok for you
to write that kind of program, but I don’t (can’t) guarantee you will
get what you expect, although I try my best. It might cost you much
to guarantee loop safety. It shouldn’t crash in any case though.
|2) Am I right in assuming that it’s possible to create an infinite loop
|this way by continually push()ing things on to items[]?
Yes, Ruby obeys you if you command it to loop infinitely.
I’m not sure what you mean by the word “right” here. It’s ok for you
to write that kind of program, but I don’t (can’t) guarantee you will
get what you expect, although I try my best. It might cost you much
to guarantee loop safety. It shouldn’t crash in any case though.
Though I was confused for half of the day today, I have to admit that
the current implementation is probably for the best. Where this:
arr.each do |x|
if y; arr.delete(x); end
end
Can be made to work the way I personally thought it would, it’s much
safer to do something like this:
delete_list = []
arr.each do |x|
if y; delete_list.push(x); end
end
arr -= delete_list
Or better yet, since Ruby includes the feature, use Array#delete_if.
Yes, Ruby obeys you if you command it to loop infinitely.
I was more asking if that’s what would happen, but I suppose the wording
of my question made its own answer
items.each do |x|
if x == “”
items.delete(x)
end
end
Wow, in Java you this would throw ConcurrentModificationException (i.e.
you can not iterate and modify a list at the same time, says Java) -
AFAIK, in java there is no workaround for this. It is interesting that
Ruby handles this as well, although i am not sure to which extent this
is (black) magic
items.each do |x|
if x == “”
items.delete(x)
end
end
Wow, in Java you this would throw ConcurrentModificationException (i.e.
you can not iterate and modify a list at the same time, says Java) -
AFAIK, in java there is no workaround for this.
Not exactly true. This special scenario is well covered by Java:
Std. collections all support remove().
IMHO iterating and deleting like shown is a very bad idea. Since
there is a method for doing deletions that’s exactly the way to go.
items.each do |x|
if x == “”
items.delete(x)
end
end
Not exactly true. This special scenario is well covered by Java:
I stand corrected. I had so many problems with similar
iterate-but-dont-touch scenarios in J that i threw remove() in the same
bag automatically ;-).
Anyway, i agree that iterating and deleting is not a good idea…
Cheers,
Peter
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.