Passes the nonexistent key as a block argument in Array#delete

```ruby
a = %w(R G B A)
p a.delete("Y") { _1 }
# BEFORE => nil (same for mruby 3.3)
# AFTER  => "Y" (same for CRuby)
```
This commit is contained in:
dearblue
2024-05-10 22:46:11 +09:00
parent c753ca33d0
commit f0cd35c78b
2 changed files with 2 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
+1
View File
@@ -449,6 +449,7 @@ 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