Skip to content

Instantly share code, notes, and snippets.

@humbroll
Created October 17, 2012 08:59
Show Gist options
  • Save humbroll/3904585 to your computer and use it in GitHub Desktop.
Save humbroll/3904585 to your computer and use it in GitHub Desktop.
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