mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Implement filter_map from Ruby2.6.
This commit is contained in:
@@ -826,5 +826,17 @@ module Enumerable
|
||||
end
|
||||
hash.values
|
||||
end
|
||||
|
||||
def filter_map(&blk)
|
||||
return to_enum(:find_index, val) unless blk
|
||||
|
||||
ary = []
|
||||
self.each do |x|
|
||||
x = blk.call(x)
|
||||
ary.push x if x
|
||||
end
|
||||
ary
|
||||
end
|
||||
|
||||
alias filter select
|
||||
end
|
||||
|
||||
@@ -188,3 +188,8 @@ assert("Enumerable#to_h") do
|
||||
assert_equal h0, h
|
||||
assert_equal({1=>4,3=>8}, c.new.to_h{|k,v|[k,v*2]})
|
||||
end
|
||||
|
||||
|
||||
assert("Enumerable#filter_map") do
|
||||
assert_equal [4, 8, 12, 16, 20], (1..10).filter_map{|i| i * 2 if i%2==0}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user