From 571f0f32c645a563b915ac8fcb1f9b3eb764da11 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 15 Dec 2016 05:47:55 +0900 Subject: Refining Array#sum monkey-patch using Refinements Because we don't want to see our ugly orig_sum method outside of this file --- activesupport/lib/active_support/core_ext/enumerable.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3