Commit 0bd99d79 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2146 from utkarshkukreti/speed-up-select-bang

Speed up Array#select! from O(n^2) to O(n).
parents 2ca5bed9 6e9a088e
......@@ -675,16 +675,11 @@ class Array
def select!(&block)
return to_enum :select! unless block_given?
idx = 0
len = self.size
while idx < self.size do
if block.call(self[idx])
idx += 1
else
self.delete_at(idx)
end
result = []
self.each do |x|
result << x if block.call(x)
end
return nil if self.size == len
self
return nil if self.size == result.size
self.replace(result)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment