-
Notifications
You must be signed in to change notification settings - Fork 29
Description
In lib/statsample.rb, the core Ruby Array class is monkey-patched to change the behavior of the sum method:
Lines 57 to 59 in 0f283e2
| def sum | |
| inject(:+) | |
| end |
Active Support (included by Rails) already defines behavior for #sum, which has slightly different behavior and can accept a block for evaluation: https://github.com/rails/rails/blob/3d716b9e66e334c113c98fb3fc4bcf8a945b93a1/activesupport/lib/active_support/core_ext/enumerable.rb#L2-L27
Similarly, the Ruby core library added the #sum method to the Enumerable class in Ruby 2.4:
- https://ruby-doc.org/core-2.4.1/Enumerable.html
- http://blog.bigbinary.com/2016/11/02/ruby-2-4-introduces-enumerable-sum.html
- ruby/ruby@41ef7ec
As such, for example, the following code works both with Active Support and/or Ruby 2.4:
> x = ['foo', 'bar']
#=> ["foo", "bar"]
> x.sum(&:bytesize)
#=> 6But fails when the statsample 2.0 library is included in the project's Gemfile:
> x = ['foo', 'bar']
#=> ["foo", "bar"]
> x.sum(&:bytesize)
#=> "foobar"Can this monkey-patching be removed and the suming functionality be done directly only where needed? This is preventing me from using statsample, unfortunately.