Skip to content

Instantly share code, notes, and snippets.

@mikedao
Created December 17, 2014 06:02
Show Gist options
  • Save mikedao/7693312a170b891cfd2f to your computer and use it in GitHub Desktop.
Save mikedao/7693312a170b891cfd2f to your computer and use it in GitHub Desktop.
Luhn's Algorithm
def validator(card)
card.chars
.map { |n| n.to_i }
.map.with_index { |n, index| index.even? ? n * 2 : n }
.map { |n| n.to_s.length == 2 ? n.to_s[0].to_i + n.to_s[1].to_i : n }
.reduce(:+) % 10 == 0 ? "Valid" : "Invalid"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment