Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize Range#sum to use arithmetic progression when a block is not …
…given [#2489]. Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
- Loading branch information
Showing
2 changed files
with
11 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1bd4d1c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not assume that a Range is a range of fixnums!
For example:
('a'..'c').sum now fails
You should return super if block_given? || !(first.instance_of?(Fixnum) || last.instance_of?(Fixnum))
1bd4d1c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marc-Andre,
It could be argued that you should know not to call sum on a range of characters, much like how you wouldn't call upcase or strip on a fixnum. However, I think it makes more sense to concatenate the characters together, much like what Array#sum does => ("a".."c").map.sum == "abc"
1bd4d1c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to ask why someone would sum something which is not numbers, but @joshuaclayton gave a good example. :)
Already provided a patch, thanks guys.