Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Enumerable#sum should not rely on size

#2489

Enumerable#sum should be valid for any Enumerable, not just those that respond_to? :size (like Array but unlike Range, for example).

My simple change maintains compatibility and doesn't rely on 'size'. I added a test attempting to sum a range which raises an exception with the previous definition of sum.

Thanks.

Reported by Marc-André Lafortune · April 14th, 2009 @ 06:27 AM

State: committed
Milestone: 2.3.4
Assigned to: nobody
Importance: none

Activity

  1. Lee Hambley
  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from enumerable, patch, sum to bugmash, enumerable, patch, sum

    August 6th, 2009 @ 02:18 PM

  3. Josh Sharpe
    Josh Sharpe

    +1, patch applies, tests pass, and makes sense to me

    August 8th, 2009 @ 05:40 AM

  4. Renaud Kern
    Renaud Kern

    patch and test pass. It works with Range, Array and Set. It's more consistent.

    August 8th, 2009 @ 11:01 AM

  5. José Valim
    José Valim

    Patch and tests pass, I'm just not sure if we should optimize ranges sum to use Arithmetic Progression sum. This way we would avoid instantiate and iterate through all the collection:

    class Range
      def sum(identity=0, &block)
        if block_given
          map(&block).sum(identity)
        else
          (last - first + 1) * (last + first) / 2
        end
      end
    end
    
    Range.new(1, 4).sum #=> 10
    Range.new(1, 10000).sum #=> 50005000
    

    August 8th, 2009 @ 11:27 AM

  6. Dan Pickett
    Dan Pickett

    +1 verified

    I like Jose's optimization idea for range as well.

    August 9th, 2009 @ 12:04 AM

  7. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to committed
    • Tag changed from bugmash, enumerable, patch, sum to enumerable, patch, sum
    • Milestone changed from 2.x to 2.3.4

    2-3-stable: 819c347f436f54acea5821289c6340da863bf138

    master: 29096268ccce2b13e1490c8b673ffe0b498555fc

    José, that'd make a nice followup patch!

    August 9th, 2009 @ 01:52 AM

  8. Repository
    Repository

    (from [29096268ccce2b13e1490c8b673ffe0b498555fc]) Enumerable#sum now works will all enumerables, even if they don't respond to :size

    [#2489 Enumerable#sum should not rely on size state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/29096268ccce2b13e1490c8b673ffe...

    August 9th, 2009 @ 05:26 AM

  9. Repository
    Repository

    (from [819c347f436f54acea5821289c6340da863bf138]) Enumerable#sum now works will all enumerables, even if they don't respond to :size

    [#2489 Enumerable#sum should not rely on size state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/819c347f436f54acea5821289c6340...

    August 9th, 2009 @ 05:26 AM

  10. Repository
  11. Repository
  12. Repository