Last active
May 16, 2017 21:04
-
-
Save JosephDunivan/cfc3204a4176eb0234cd8d0f67c30a22 to your computer and use it in GitHub Desktop.
Ruby Adds Digits of An Integer
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
def add_digits(n) | |
#maps characters of a string and converts it back to integers in an array | |
sum = n.to_s.chars.map(&:to_i) | |
#collapses the array by adding elements together. Same as inject. | |
sum.reduce(:+) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment