How about this one?
n = 2011
n.size.pred.downto(0).reduce(0) { |sum, a| sum += (n.abs / 10 ** a) % 10 }
On Nov 30, 2011, at 3:10 PM, James Gallagher wrote:
> Hi everyone.
>
> I have a really stupid question here, but i'm a newbie so hear me out.
>
> I want to add together all the digets in a number.
> (for example, 2011 would be 4).
> but i want to do it with a program.
>
> I don't know how to split the individual digits up.
>
> Thank you so much.
>
> James. :-)
>
> --
> Posted via http://www.ruby-forum.com/.
>