From: "Mauricio Fernández" Date: 2005-04-20T17:30:18+09:00 Subject: Re: [PATCH] Trivial speedup in Array#zip On Wed, Apr 20, 2005 at 11:12:55AM +0900, daz wrote: > > On Wed, Apr 20, 2005 at 07:02:24AM +0900, Nobu wrote: > > > `tmp' can be modified or stored anywhere while yielding, [...] > > > > I see. What about this? > > > > [snip "this"] > > I guess Nobu's answer remains the same ? [...] > arr = [] > [1,2,3].zip([4,5,6]){|x| arr.push(x) if x.include?(4)} > p arr #-> [[1, 4]] > > With your change (reusing 'tmp'), the stored reference in arr > would dynamically change throughout the zip loop and the final > (very incorrect) value in arr would be [[3, 6]]. > (Only one value was pushed by the code block, however, and it > was the correct result at that time). "this" should handle that correctly: batsman@tux-chan:/tmp/ruby$ ./ruby arr = [] [1,2,3].zip([4,5,6]){|x| arr.push(x) if x.include?(4)} p arr [[1, 4]] batsman@tux-chan:/tmp/ruby$ ruby arr = [] [1,2,3].zip([4,5,6]){|x| arr.push(x) if x.include?(4)} p arr [[1, 4]] -- Mauricio Fern�ndez