Skip to content

Instantly share code, notes, and snippets.

@juzow13
Created October 13, 2017 12:27
Show Gist options
  • Save juzow13/c0eea1b5fc253ad5a19ea9a3b9ba430b to your computer and use it in GitHub Desktop.
Save juzow13/c0eea1b5fc253ad5a19ea9a3b9ba430b to your computer and use it in GitHub Desktop.
convert isbn13 to isbn10
# isbn is isbn13
def to_isbn10(isbn)
digits = isbn.slice(3,9).split("")
sum = 0
(0..9).each do |i|
sum += digits[i].to_i * (10 - i)
end
chk_tmp = 11 - (sum % 11)
chk_digit = ""
if chk_tmp == 10
chk_digit = 'x'
elsif chk_tmp == 11
chk_digit = 0
else
chk_digit = chk_tmp
end
digits.push(chk_digit)
return digits.join("")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment