Permalink
Browse files

Remove unnecessary &block from Range#sum and add tests for (num..floa…

…t).sum
  • Loading branch information...
lifo committed Aug 9, 2009
1 parent 97a5c7a commit 60bed59fbf21fcfe5b9a753181f6d34488055acd
@@ -115,7 +115,7 @@ def none?(&block)
class Range #:nodoc:
# Optimize range sum to use arithmetic progression if a block is not given and
# we have a range of numeric values.
- def sum(identity=0, &block)
+ def sum(identity = 0)
return super if block_given? || !(first.instance_of?(Integer) && last.instance_of?(Integer))
actual_last = exclude_end? ? (last - 1) : last
(actual_last - first + 1) * (actual_last + first) / 2
@@ -66,6 +66,7 @@ def test_enumerable_sums
assert_equal 10, (1..4).sum
assert_equal 6, (1...4).sum
assert_equal 'abc', ('a'..'c').sum
+ assert_raises(NoMethodError) { 1..2.5.sum }
@arthurschreiber

arthurschreiber Aug 9, 2009

I think this test is wrong, as you're missing the parentheses around the range. #sum is called on 2.5 here.

end
def test_each_with_object

1 comment on commit 60bed59

Member

lifo commented on 60bed59 Aug 9, 2009

Yup. That was fixed already.

Please sign in to comment.