On 31.07.2008, at 12:23, Sebastian Hungerecker wrote:

> Web Reservoir wrote:
>> Now when you type RUBY you should get the total as 9+3+2+7 = (21)  
>> 2+1 =
>> 3
>> where R = 9, U = 3, B = 2 and y = 7 ( as shown above )
>> ...
>> I would like to code this in Ruby. Pl. help me with this code.
>
>
>>> "RUBY".unpack("C*").inject(0) {|s,x| s + (x-65)%9 +
> 1}.to_s.split(//).inject(0) {|s,x| s + x.to_i}
> => 3

Nice code!

"RUBY".unpack("C*").inject(0) { |s,x| s + (x-64)%9 }%9
=> 3

whouldn't that be better?

regards
Karl-Heinz