Skip to content

Instantly share code, notes, and snippets.

@JosephDunivan
Last active May 16, 2017 21:04
Show Gist options
  • Save JosephDunivan/cfc3204a4176eb0234cd8d0f67c30a22 to your computer and use it in GitHub Desktop.
Save JosephDunivan/cfc3204a4176eb0234cd8d0f67c30a22 to your computer and use it in GitHub Desktop.
Ruby Adds Digits of An Integer
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