Skip to content

Instantly share code, notes, and snippets.

@nocd5
Created November 29, 2015 15:23
Show Gist options
  • Save nocd5/cfdfa2649a681998f5e5 to your computer and use it in GitHub Desktop.
Save nocd5/cfdfa2649a681998f5e5 to your computer and use it in GitHub Desktop.
def get_check_digit (number)
sum = 0
number[0,11].reverse.split(//).each_with_index {|n, i|
sum += n.to_i * (i % 6 + 2)
}
return sum <= 1 ? 0 : 11 - sum % 11
end
puts "Please input your number"
number = gets.chomp
if number.length != 12 then
puts "Invalid number"
exit
else
if get_check_digit(number) == number[11].to_i then
puts "Check Digit is valid"
else
puts "Check Digit is NOT valid"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment