in reply to code that fails to act as expected...

The hidden trick is that "each" behaves as if it remembers how many times you called it, or more specifically, does something like:

First time call: build an array of keys in even positions and their values in odd positions and then...

All calls: shift and return the first two values in stored array.

The single-operand for loop you show calls "each" only once irrespective of what is returned and so it builds an iteration list of only one key/value pair. The reason a while loop DWIMs on the other hand is that it keeps calling "each", picking up pairs until the array it "kept to itself" is exhausted.

__________________________________________________________________________________

^M Free your mind!

  • Comment on Re: code that fails to act as expected...