Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Created November 26, 2013 01:09
Show Gist options
  • Save jgaskins/7651822 to your computer and use it in GitHub Desktop.
Save jgaskins/7651822 to your computer and use it in GitHub Desktop.
Remove one and only one occurrence of each specified value from an array
class Array
def remove_one values
values = values.dup
new_array = []
each do |value|
if values.include? value
values.delete value
else
new_array << value
end
end
new_array
end
end
describe 'Array#remove_one' do
it 'removes one of each value from the array' do
[1, 1, 2, 3].remove_one([1, 2]).should == [1, 3]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment