Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Digit sum in Ruby 1.9.2
def digit_sum(n)
n.to_s.each_char.map {|c| c.to_i }.reduce(:+)
end

Shorter and more understandable with:

n.to_s.chars.map(&:to_i).reduce(:+)

what about this one:
n.to_s.chars.inject { |sum,n| sum = sum.to_i + n.to_i}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment