Skip to content

Commit 066c8f5

Browse files
committed
Add Array#delete_first, Array#delete_first_each
1 parent 17138ae commit 066c8f5

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

lib/core/facets/array/delete_first.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Array
2+
# Deletes first item from +self+ that is equal to +obj+.
3+
#
4+
# Returns the deleted item, or +nil+ if no matching item is found.
5+
#
6+
# If the optional code block is given, the result of the block is returned if
7+
# the item is not found.
8+
#
9+
# a = [ "a", "b", "b", "b", "c" ]
10+
# a.delete_first("b") #=> "b"
11+
# a #=> ["a", "c"]
12+
# a.delete("z") #=> nil
13+
# a.delete("z") {"not found"} #=> "not found"
14+
def delete_first(el)
15+
i = index(el)
16+
if i
17+
delete_at(i)
18+
end
19+
end
20+
end
21+

lib/core/facets/array/remove.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Array
2+
# Returns a new array that is a copy of the original array, removing the first occurrence of any
3+
# item that also appears in +other_ary+. The order is preserved from the original array.
4+
#
5+
# If there are multiple occurrences of an item in +other_ary+, this removes that many occurrences
6+
# from self.
7+
#
8+
# This is similar to `Array#-` and `Array#difference`, except that instead of removing _all_
9+
# matches, it only removes as many occurrences as there are in the +other_ary+:
10+
#
11+
# > [1, 1, 2].remove [1]
12+
# => [1, 2]
13+
#
14+
# > [1, 1, 2].remove [1, 1, 1]
15+
# => [2]
16+
#
17+
# > [1, 1, 2] - [1]
18+
# => [2]
19+
#
20+
# > [1, 1, 2].difference [1]
21+
# => [2]
22+
#
23+
def remove(other_ary)
24+
dup.remove!(other_ary)
25+
end
26+
27+
# For each element of `other_ary`, deletes the _first_ element from self that is equal to that element
28+
# (using `delete_at`, not `delete`).
29+
#
30+
# This is the in-place version of remove.
31+
#
32+
def remove!(other_ary)
33+
other_ary.each do |el|
34+
delete_first(el)
35+
end
36+
self
37+
end
38+
end

test/core/array/test_remove.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
covers 'facets/array/remove'
2+
3+
test_case Array do
4+
method :remove do
5+
test do
6+
a = [1,1,2,3]
7+
a.difference([1]).assert == [ 2,3]
8+
(a - [1]).assert == [ 2,3]
9+
a.remove( [1]).assert == [ 1,2,3]
10+
a.assert == [1,1,2,3]
11+
end
12+
13+
test do
14+
a = [1,1,2,3]
15+
(a - [1]).assert == [ 2,3]
16+
a.remove([1, 1, 1]).assert == [ 2,3]
17+
a.assert == [1,1,2,3]
18+
end
19+
20+
test 'example from Array#- docs' do
21+
a = [ 1, 1, 2, 2, 3, 3, 4, 5 ]
22+
(a - [ 1, 2, 4 ]).assert == [ 3, 3, 5 ]
23+
a.remove([1, 2, 4 ]).assert == [ 1, 2, 3, 3, 5 ]
24+
end
25+
26+
test 'element that is not found' do
27+
a = [1,1,2,3]
28+
(a - [4]).assert == [1,1,2,3]
29+
a.remove([4]).assert == [1,1,2,3]
30+
a.assert == [1,1,2,3]
31+
end
32+
end
33+
34+
method :remove! do
35+
test do
36+
a = [1,1,2,3]
37+
a.remove!([1]).assert == [ 1,2,3]
38+
a.assert == [ 1,2,3]
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)