Skip to content
  • Jeremy Daer's avatar
    1f3d640c
    Ruby 2.4: compat with new Array#sum · 1f3d640c
    Jeremy Daer authored
    Ruby 2.4 introduces `Array#sum`, but it only supports numeric elements,
    breaking our `Enumerable#sum` which supports arbitrary `Object#+`.
    To fix, override `Array#sum` with our compatible implementation.
    
    Native Ruby 2.4:
    
        %w[ a b ].sum
        # => TypeError: String can't be coerced into Fixnum
    
    With `Enumerable#sum` shim:
    
        %w[ a b ].sum
        # => 'ab'
    
    We tried shimming the fast path and falling back to the compatible path
    if it fails, but that ends up slower even in simple causes due to the cost
    of exception handling. Our only choice is to override the native `Array#sum`
    with our `Enumerable#sum`.
    1f3d640c
    Ruby 2.4: compat with new Array#sum
    Jeremy Daer authored
    Ruby 2.4 introduces `Array#sum`, but it only supports numeric elements,
    breaking our `Enumerable#sum` which supports arbitrary `Object#+`.
    To fix, override `Array#sum` with our compatible implementation.
    
    Native Ruby 2.4:
    
        %w[ a b ].sum
        # => TypeError: String can't be coerced into Fixnum
    
    With `Enumerable#sum` shim:
    
        %w[ a b ].sum
        # => 'ab'
    
    We tried shimming the fast path and falling back to the compatible path
    if it fails, but that ends up slower even in simple causes due to the cost
    of exception handling. Our only choice is to override the native `Array#sum`
    with our `Enumerable#sum`.
Loading