From: Bruce Woodward Date: 2006-04-13T21:14:12+09:00 Subject: Re: map_if, collect_if ??? ------=_Part_18803_17340494.1144930447706 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Robert and Logan, Thanks for your replies. That was good thinking. On 4/13/06, Robert Dober wrote: > > On 4/13/06, Bruce Woodward wrote: > > module Enumerable > def map_if( &block ) > find_all(&block).map(&block) > end > end I particularly like the use of calling block twice. At first I thought that this couldn't work but of course once the block has been successful for the call to find_all, it must be successful for the call to map -- very nice. I will file away the find_all {}.map {} idiom for future use but for this case I will either use a manual accumulator or inject. The only reason is performance and performance only matters in this case because the code is likely to be get call many hundreds of times in a loop and I will be impatiently waiting for it finish. WRT to performance, not only is the block being called twice but an extra array is being created and then freed. In the past I have had issues ruby processes using a lot of memory and spending time with the garbage collector. I don't want to start any kind of anti-ruby flame war here; Rub= y is my first choice. Manually stopping the GC at the top of the loop and starting the GC at the bottom of the loop, solved the problem but I just want to minimse memory use where I can, just for this code. thanks again. Bruce. ------=_Part_18803_17340494.1144930447706--