about summary refs log tree commit diff
path: root/lib/enumerable.rb
blob: 66918f65e8b132ea158904f56edd3046947ab7a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# frozen_string_literal: true

module Enumerable
  # TODO: Remove this once stop to support Ruby 2.6
  if RUBY_VERSION < '2.7.0'
    def filter_map
      if block_given?
        result = []
        each do |element|
          res = yield element
          result << res if res
        end
        result
      else
        Enumerator.new do |yielder|
          result = []
          each do |element|
            res = yielder.yield element
            result << res if res
          end
          result
        end
      end
    end
  end
end