From: pharrington Date: 2010-01-02T04:20:15+09:00 Subject: Re: Delete elements in array, break, and keep changes? On Jan 1, 2:18 pm, pharrington wrote: > On Jan 1, 11:45 am, Joe Buck wrote: > > > > > > > Ehsan, > > > Thanks for the help. Unfortunately I don't think that will work as it > > removes the first element in the array. > > > What I need is an Array::delete_if or Array::reject! call that allows me > > to break from the block while still removing elements. > > > My solution is to use a second array to keep the indices of all removed > > elements. I can then iterate through the second array after the first > > loop and remove the elements. I'm guessing reject! and delete_if do this > > as well. It could be more efficient but it works. > > > Thanks! > > -- > > Posted viahttp://www.ruby-forum.com/. > > Without knowing the point of what you're trying to do, this sounds > like what you have in mind: > > array = [] # or whatev > process_array = true > array.reject!(x) do >   if process_array >     conditional_code_for x >   else >     false > end Also, is the reject! really the bottleneck in your app? We obviously don't want to intentionally write sub-optimal code, but do not optimize what's not slowing you down.