One more way to do it: mutually recursive functions that each strip
off the last character and alternate the doubling:
http://pastie.caboo.se/58669
or
def luhn_sum s
val = s.slice!(-1)
if val==nil
return 0
end
val.chr.to_i + luhn_helper( s)
end
def luhn_helper s
val = s.slice!(-1)
if val==nil
return 0
end
(2 * val.chr.to_i).to_s.split("").inject(0){|sum,x|sum + x.to_i} +
luhn_sum( s)
end
def luhn? s
luhn_sum(s) % 10 == 0
end
Regards,
Paul.