From: pharrington Date: 2010-01-02T04:30:04+09:00 Subject: Re: Delete elements in array, break, and keep changes? On Jan 1, 2:19 pm, pharrington wrote: > 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. wow array.reject! do |x| i don't know basic ruby syntax today... Or if you really want to avoid even iterating over each element: original_array = [] new_array = [] original_array.each {|x| new_array << x unless conditional_code_for (x)} But again, what precisely are you doing? Is the deletion code really your bottleneck? And if it is, I'm almost certain that using a more appropriate structure than an array would be the way to go.