diff options
author | Akira Matsuda <ronnie@dio.jp> | 2016-12-15 05:47:55 +0900 |
---|---|---|
committer | Akira Matsuda <ronnie@dio.jp> | 2016-12-15 05:48:23 +0900 |
commit | 571f0f32c645a563b915ac8fcb1f9b3eb764da11 (patch) | |
tree | cf5f24737d9060d8946d28daf96aa1f6874630ee | |
parent | 1f0fe334f577479ad524cb990bef4454585857c0 (diff) | |
download | rails-571f0f32c645a563b915ac8fcb1f9b3eb764da11.tar.gz rails-571f0f32c645a563b915ac8fcb1f9b3eb764da11.tar.bz2 rails-571f0f32c645a563b915ac8fcb1f9b3eb764da11.zip |
Refining Array#sum monkey-patch using Refinements
Because we don't want to see our ugly orig_sum method outside of this file
-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 |