From: dblack@... Date: 2006-01-29T23:35:13+09:00 Subject: Re: Loop weirdness Hi -- On Sun, 29 Jan 2006, Jonathan Leighton wrote: > Okay, so let me get this right... if you do > > foo.each { } > > foo is re-evaluated with each iteration? Hence the bug? Why? The length of foo is re-evaluated each time; that's how the decision is made whether or not to keep iterating. Array#each essentially works like this: def each i = 0 while i < length yield self[i] i += 1 end self end So if the length of the array increases in mid-stream, the iteration will continue, based on the while test. You can always get around this with something like: array.length.times do |i| # do something with array[i] end where length won't be re-evaluated. David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming May 1, 2006! http://www.manning.com/books/black