Skip to content

Instantly share code, notes, and snippets.

@FrendaWinter
Created November 6, 2023 09:22
Show Gist options
  • Save FrendaWinter/3b9e7587db3881c40e709a857db559b0 to your computer and use it in GitHub Desktop.
Save FrendaWinter/3b9e7587db3881c40e709a857db559b0 to your computer and use it in GitHub Desktop.
Check if the creadit card is vaild with Ruby
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'creditcard'
# Usage: checkValidCreditCard.rb [card_number_0] [card_number_1] [card_number_???]
puts "Usage: checkValidCreditCard.rb [card_number_0] [card_number_1] [card_number_???]" if ARGV.empty?
index = 0
while (card_number = ARGV.shift)
if card_number.creditcard?
puts "---------------------------"
puts "Card #{index}: #{card_number}"
puts '=> Valid: True'
puts "=> Type: #{card_number.creditcard_type}"
puts "---------------------------"
else
puts "---------------------------"
puts "Card #{index}: ${card_number}"
puts '=> Valid: True'
puts '=> Valid: False'
puts "---------------------------"
end
index += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment