Created
October 19, 2018 02:39
-
-
Save mediter/0bbd44c2955c60cfa515744cbfa0ac9c to your computer and use it in GitHub Desktop.
[Ruby Array Sum] #ruby #iterator #sum
This file contains hidden or 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
# you can call #sum iterator on it: | |
numbers = [1, 2, 3, 4, 5] | |
numbers.sum | |
# => 15 | |
# N.B: In Ruby versions < 2.4, sum didn't exist. The idiomatic way to get the same result was with #reduce: | |
numbers.reduce(:+) | |
# => 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment