Skip to content
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

Fix Enumerable#sum redefined warning #28781

Merged
merged 1 commit into from
Apr 18, 2017
Merged

Fix Enumerable#sum redefined warning #28781

merged 1 commit into from
Apr 18, 2017

Commits on Apr 18, 2017

  1. Fix Enumerable#sum redefined warning

    If we require 'active_support/core_ext/enumerable' on Ruby 2.4,
    we'll see following warning because `Enumerable#sum` and `Array#sum`
    are added in Ruby 2.4.
    
    ```
    rails/rails/activesupport/lib/active_support/core_ext/enumerable.rb:20: warning: method redefined; discarding old sum
    ```
    
    The minimal way to fix the warning is `alias sum sum`.
    
    ```
    $ ruby -v
    ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
    
    $ ruby -w -e "def a; end; def a; end"
    -e:1: warning: method redefined; discarding old a
    -e:1: warning: previous definition of a was here
    
    $ ruby -w -e "def a; end; alias a a; def a; end"
    ```
    
    But this behavior is not intended. (@amatsuda was told by @ko1)
    So we should use `alias` as a meaningful way.
    
    Ruby 2.4's `sum`  (`orig_sum`) assumes an `identity` is `0` when we omit `identity`
    so we can delegate to `orig_sum` with explicit `identity` only.
    In a strict sense, we can detect `identity` by check instance's class
    but we don't care at this time about that because calling `Enumerable#sum` is rare.
    In many cases, we will call `Array#sum`.
    mtsmfm committed Apr 18, 2017
    Configuration menu
    Copy the full SHA
    3adbf14 View commit details
    Browse the repository at this point in the history