Project

General

Profile

Bug #13700

Updated by sos4nt (Stefan Schüßler) over 6 years ago

`Enumerable#sum` is optimized for integer ranges. Unfortunately, this can break subclasses: 

 ```ruby 
 class StepTwoRange < Range 
   def each(&block) 
     step(2, &block) 
   end 
 end 

 r = StepTwoRange.new(0, 10) 
 r.to_a       #=> [0, 2, 4, 6, 8, 10] 

 r.to_a.sum #=> 30 
 r.sum        #=> 55 
 ``` 

 The optimization should therefore only be triggered for instances of `Range` and not for instances of subclasses. 

 If this behavior is intentional, it should at least be documented.

Back