Skip to content

Instantly share code, notes, and snippets.

@junaruga
Created August 28, 2023 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junaruga/2d9df494bed72d4a1de98b12e83530d1 to your computer and use it in GitHub Desktop.
Save junaruga/2d9df494bed72d4a1de98b12e83530d1 to your computer and use it in GitHub Desktop.
$ cat card.rb
# Set your card number here. 16 digits.
card_no = '1234123412341234'
reverse_card_no = card_no.reverse
index = 1
total = 0
reverse_card_no.each_char do |c|
multi = (index % 2 == 1) ? 1 : 2
sub_total = c.to_i * multi
sub_total -= 9 if sub_total >= 10
total += sub_total
index += 1
end
puts "total: #{total}"
if total % 10 == 0
puts "Valid card no."
else
puts "Invalid card no."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment