Please Forward: Ruby Quiz Submission

From: Paolo Bonzini <bonzini@gnu.org>
Date: September 12, 2007 7:31:31 AM CDT
To: submission@rubyquiz.com
Subject: Please Forward: Ruby Quiz Submission

This is a solution for rubyquiz #122 (credit card).

class String
  def luhn
    (split(//).inject([0,0]) {
      >(s1,s2), ch| c=ch.to_i; [s2 + c, s1 + (c*2%10) + (c*2/10)]
    }) [0] % 10 == 0
  end
end

mapn is probably more elegant, but this one does not use reverse, which is pretty cool IMO. :slight_smile:

Paolo

···

Begin forwarded message: