From: Saku Ytti Date: 2008-04-12T15:55:56+09:00 Subject: Re: delete_if, select and (lack of) delete_if!, self.POLS == false On (2008-04-12 08:27 +0900), ara.t.howard wrote: >> foo = {} >> ('a'..'z').each { |l| foo[l] = somearray.select {dosomething} } >> > [ 'a', 'a', 'a' ].select{|x| x == 'a'} > > and you've clobbered.... not to mention it loses ordering. the only > thing which has semantics inline with that of select is > > a.replace a.select(&condition) > > which preserves ordering and allows for duplicates. Yes but if condition needs variable from outer loop (that is, in my example, you'd use the 'l' from Range object in the 'dosomething' block, so you're walking through the array multiple times. If you positively know, you want to pick each element exactly once to a new array/hash. Then having select that reduces the array size by not forcing you to look at elements you know you're not going to pick up (as they were already picked up by earlier condition, to a earlier hash/array, and you know, no future condition can any more be true for them). If array would be extremely large, the loop would get the faster the closer to end it is, as array would keep shrinking. Thanks, -- ++ytti