1
0

enumerable.rb 545 B

1234567891011121314151617181920212223242526
  1. # frozen_string_literal: true
  2. module Enumerable
  3. # TODO: Remove this once stop to support Ruby 2.6
  4. if RUBY_VERSION < '2.7.0'
  5. def filter_map
  6. if block_given?
  7. result = []
  8. each do |element|
  9. res = yield element
  10. result << res if res
  11. end
  12. result
  13. else
  14. Enumerator.new do |yielder|
  15. result = []
  16. each do |element|
  17. res = yielder.yield element
  18. result << res if res
  19. end
  20. result
  21. end
  22. end
  23. end
  24. end
  25. end