0
0
Fork 0

Optimize map { ... }.compact calls (#15513)

* Optimize map { ... }.compact

using Enumerable#filter_map, supported since Ruby 2.7

* Add poyfill for Enumerable#filter_map
This commit is contained in:
luigi 2021-01-09 18:32:01 -05:00 committed by GitHub
parent 9395143126
commit 087ed84367
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 52 additions and 26 deletions

26
lib/enumerable.rb Normal file
View file

@ -0,0 +1,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

View file

@ -27,7 +27,7 @@ module Paperclip
return true if original_filename == other_filename
return false if original_filename.nil?
formats = styles.values.map(&:format).compact
formats = styles.values.filter_map(&:format)
return false if formats.empty?