Merge pull request #6148 from buty4649/add-enumerable-grep_v

Add Enumerable#grep_v to mruby-enum-ext
This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-01-21 18:51:11 +09:00
committed by GitHub
2 changed files with 18 additions and 0 deletions
+11
View File
@@ -840,6 +840,17 @@ module Enumerable
alias filter select
def grep_v(pattern, &block)
ary = []
self.each{|*val|
sv = val.__svalue
unless pattern === sv
ary.push((block)? block.call(*val): sv)
end
}
ary
end
##
# call-seq:
# enum.tally -> a_hash
+7
View File
@@ -195,3 +195,10 @@ end
assert("Enumerable#tally") do
assert_equal({"a"=>1, "b"=>2, "c"=>1}, ["a", "b", "c", "b"].tally)
end
assert("Enumerable#grep_v") do
a = [1, 2, 3, 4, 5, 0]
assert_equal [1, 5, 0], a.grep_v(2..4)
assert_equal [1, 2, 3, 4, 5, 0], a.grep_v(6..8)
assert_equal [2, 4, 6, 8, 10], a.grep_v(0) {|v| v * 2}
end