Skip to content

Instantly share code, notes, and snippets.

@vicgc
Forked from woodrow/gist:6347926
Created June 25, 2014 01:17
Show Gist options
  • Save vicgc/c8ac791d3bfed8a84c8e to your computer and use it in GitHub Desktop.
Save vicgc/c8ac791d3bfed8a84c8e to your computer and use it in GitHub Desktop.
def compute_check_digit(str)
str = str.strip.upcase
values = str.chars.map do |char|
case char
when '<'
0
when 'A'..'Z'
char.ord - 65 + 10
when '0'..'9'
char.ord - 48
else
raise "Unexpected character '#{char}'"
end
end
values.zip([7,3,1].cycle).map{|(v,w)| v * w}.reduce(:+) % 10
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment