Created
October 17, 2012 08:59
-
-
Save humbroll/3904585 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def creditcard_number_luhn_validator(card_num, check_num) | |
s1 = "" | |
card_num.split(//).map(&:to_i).reverse.each_with_index do |n, i| | |
s1 << ((i%2 == 1)? (n*2) : n) | |
end | |
s2 = s1.split(//).inject(0){|sum,c| sum + c.to_i} | |
s3 = (s2.divmod(10)[0]+1)*10 - s2 | |
s4 = (s3 == check_num) | |
end | |
puts creditcard_number_luhn_validator("4579730322257044", 10)? "valid" : "invalid" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment