-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monkey-patched Array#sum
method changes/breaks Ruby 2.4 method functionality
#58
Comments
🤦♂️ |
Can you send a PR with the monkey patching removed and replace instances of |
I don't have the time myself to fix this, unfortunately. |
We had this issue as well - We simply patched over it again after requiring statsample - not ideal (or clean) but solved the issue for us. Imo It would be best to avoid global monkey patch - they will have unintended side-effects and break other ruby functions and make it hard to debug when they go wrong (I had no idea it was Statsample monkey-patching when I first saw this issue)... Any chance this could be implemented as a Refinement - Alternatively, at the very least, put them into a separate class/module and then use inheritance to monkey-patch. One potential implementation would be: module StatSample
module ArrayExtensions
def sum
....
end
end
end
class Array
include StatSample::ArrayExtensions
end See https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/ |
Don't define Array#sum if already defined. Fixes #58
In
lib/statsample.rb
, the core RubyArray
class is monkey-patched to change the behavior of thesum
method:statsample/lib/statsample.rb
Lines 57 to 59 in 0f283e2
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-L27Similarly, 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:
But fails when the
statsample
2.0 library is included in the project's Gemfile:Can this monkey-patching be removed and the
sum
ing functionality be done directly only where needed? This is preventing me from usingstatsample
, unfortunately.The text was updated successfully, but these errors were encountered: