From: Yukihiro Matsumoto Date: 2006-04-20T08:00:24+09:00 Subject: Re: Array#each Looping Gotcha Hi, In message "Re: Array#each Looping Gotcha" on Thu, 20 Apr 2006 07:43:46 +0900, Nathan Olberding 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. matz.