From: Garance A Drosehn Date: 2007-02-10T08:11:16+09:00 Subject: Re: Jumping to "the next one" in something#each ------=_Part_18579_7739956.1171062672405 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 2/9/07, Philip Hallstrom wrote: > > Garance wrote: > > ...Let's say I want to have > > something like: > > > > ARGV.each { |arg| > > case arg > > when "-f" > > ...do something with the *next* value of |arg|... > > end > > } > > > > What I do is set some variable to remember that I'm in the middle > > of "-f" processing, and then catch that when the code-block is > > executed for the next value. For example: > > Well, if you don't mind trashing the array you're processing you could use > Array.shift to remove the first element and return it... then just > re-order your loop to exit once the element returns is nil. That's not a general solution, though. Consider an example of: filetree.added_files.keys.sort.each { |fname| ...whatever... } I'd have to create a temp-array to hold the result from the sort step. For that matter, I never want to *destroy* an array when I'm looping through the values in it. If I'm using the 'each' method, then the array-object is never modified. Besides, I wanted something which works for *any* routine which takes a code-block, not just objects of type Array. How could you make that work for the each_pair method of Hash objects? I was thinking there should be some "big-picture" solution possible, one which would work for all methods which take a code-block parameter. That is probably too complicated a goal, but at least a solution which would work for some large subset of those methods. -- Garance Alistair Drosehn = drosihn@gmail.com Senior Systems Programmer Rensselaer Polytechnic Institute; Troy, NY; USA ------=_Part_18579_7739956.1171062672405--