Skip to content

Commit

Permalink
Merge pull request #25008 from lvl0nax/as_enumarable_index_by_refacto…
Browse files Browse the repository at this point in the history
…ring

Perfomance fix for ActiveSupport Enumerable#index_by
  • Loading branch information
rafaelfranca committed May 14, 2016
2 parents b981369 + 6751b10 commit 4046ac7
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion activesupport/lib/active_support/core_ext/enumerable.rb
Expand Up @@ -34,7 +34,9 @@ def sum(identity = nil, &block)
# => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
def index_by
if block_given?
Hash[map { |elem| [yield(elem), elem] }]
result = {}
each { |elem| result[yield(elem)] = elem }
result
else
to_enum(:index_by) { size if respond_to?(:size) }
end
Expand Down

0 comments on commit 4046ac7

Please sign in to comment.