In Ruby 2.4 and for Integer Ranges, Range(Enumerable)#sum
is optimized to return a result directly, without iterating over all elements.
I don't understand why the corresponding code is defined in enum.c
for the Enumerable
module and not in range.c
for Range
class.
Why should Enumerable
know about classes that include it (e.g. Range
, Hash
, ...) and check for their type instead of letting those classes overwrite Enumerable#sum
?
Seen in enum.c
:
return int_range_sum(beg, end, excl, memo.v);
# or
hash_sum(obj, &memo);