Merge pull request #6266 from dearblue/array-delete

Passes the nonexistent key as a block argument in `Array#delete`
This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-05-14 09:49:36 +09:00
committed by GitHub
2 changed files with 14 additions and 1 deletions
+1 -1
View File
@@ -193,7 +193,7 @@ class Array
def delete(key, &block)
len = self.length
ret = self.__delete(key)
return block&.call() if len == self.length
return block&.call(key) if len == self.length
ret
end
+13
View File
@@ -445,3 +445,16 @@ assert('Array#freeze') do
a[0] = 1
end
end
assert('Array#delete') do
a = ["a", "b", "c"]
assert_equal nil, a.delete("x")
assert_equal "x", a.delete("x") { _1 }
assert_equal ["a", "b", "c"], a
assert_equal "a", a.delete("a")
assert_equal ["b", "c"], a
a = [nil]
assert_equal nil, a.delete(nil) { "?" }
assert_equal [], a
end