diff options
author | Semyon Perepelitsa <sema@sema.in> | 2012-01-15 11:42:31 +0800 |
---|---|---|
committer | Semyon Perepelitsa <sema@sema.in> | 2012-01-15 11:42:31 +0800 |
commit | d696f8de92ceb3cb38b11ec3b200b082b9578742 (patch) | |
tree | efd81f0e921ad669e549286035aa6e6056086785 /activesupport | |
parent | 78ef738313d66226854b688db6ab256d078b40bb (diff) | |
download | rails-d696f8de92ceb3cb38b11ec3b200b082b9578742.tar.gz rails-d696f8de92ceb3cb38b11ec3b200b082b9578742.tar.bz2 rails-d696f8de92ceb3cb38b11ec3b200b082b9578742.zip |
Pass a symbol instead of a block. This is faster and more concise.
At least Ruby 1.8.7 is required which is ok since 3.2.
Benchmark:
```ruby
require "benchmark"
enum = 1..10_000
N = 100
Benchmark.bm do |x|
x.report "inject block" do
N.times do
enum.inject { |sum, n| sum + n }
end
end
x.report "inject symbol" do
N.times do
enum.inject(:+)
end
end
end
```
Result:
```
user system total real
inject block 0.160000 0.000000 0.160000 ( 0.179723)
inject symbol 0.090000 0.000000 0.090000 ( 0.095667)
```
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 9343bb7106..ae8de945d0 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -59,7 +59,7 @@ module Enumerable if block_given? map(&block).sum(identity) else - inject { |sum, element| sum + element } || identity + inject(:+) || identity end end |