diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2017-01-03 13:50:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-03 13:50:57 -0500 |
commit | a194ec88feef53ce0f2979635328df8d817ffe4d (patch) | |
tree | fa0ccc9cd6506704edf49450b6ae6149f6a7f147 /activesupport/lib | |
parent | 058eb959bff8e43953f435bdf25d35435440d09e (diff) | |
parent | 571f0f32c645a563b915ac8fcb1f9b3eb764da11 (diff) | |
download | rails-a194ec88feef53ce0f2979635328df8d817ffe4d.tar.gz rails-a194ec88feef53ce0f2979635328df8d817ffe4d.tar.bz2 rails-a194ec88feef53ce0f2979635328df8d817ffe4d.zip |
Merge pull request #27363 from amatsuda/refined_array_sum
Refining Array#sum monkey-patch using Refinements
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 8ebe758078..ae08fd90c1 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -115,9 +115,14 @@ end # and fall back to the compatible implementation, but that's much slower than # just calling the compat method in the first place. if Array.instance_methods(false).include?(:sum) && !(%w[a].sum rescue false) - class Array - alias :orig_sum :sum + # Using Refinements here in order not to expose our internal method + using Module.new { + refine Array do + alias :orig_sum :sum + end + } + class Array def sum(init = nil, &block) #:nodoc: if init.is_a?(Numeric) || first.is_a?(Numeric) init ||= 0 |