Skip to content

Monkey-patched Array#sum method changes/breaks Ruby 2.4 method functionality #58

@sjdemartini

Description

@sjdemartini

In lib/statsample.rb, the core Ruby Array class is monkey-patched to change the behavior of the sum method:

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:

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)
#=> 6

But 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions