Skip to content

Instantly share code, notes, and snippets.

@niku
Created April 4, 2010 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niku/355023 to your computer and use it in GitHub Desktop.
Save niku/355023 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class Array
def delete_each target
self.inject([]) do |result, check|
if index = target.find_index(check)
target.delete_at(index)
else
result << check
end
result
end
end
end
if __FILE__ == $0
ary = [1,1,1,2,3,4,4,5]
p ary
# => [1, 1, 1, 2, 3, 4, 4, 5]
p "ary - [1,2,3] = #{ary - [1,2,3]}"
# => [4,4,5]
# oops.. I expect get [1,1,4,4,5]
p "ary.delete_each [1,2,3] = #{ary.delete_each [1,2,3]}"
# => [1, 1, 4, 4, 5]
# OK :)
p "ary.delete_each [1,4,5] = #{ary.delete_each [1,4,5]}"
# => [1, 1, 2, 3, 4]
p "ary.delete_each [6] = #{ary.delete_each [6]}"
# => [1, 1, 1, 2, 3, 4, 4, 5]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment