Calculate ISIN check-digit in Ruby

The check digit in ISIN is calculated with a variant of the Luhn algorithm.

Also check out my page in swedish on how to validate a swedish personal identity number.

All source code on this page is published under the MIT License as stipulated here.


The isin parameter should be the ISIN without the check digit.

# jonelf¤ 2009-10-14
def calc_ISIN_checksum(isin)
  raise isin + " should be 11 characters" if isin.length!=11
  isin.split(//).map \
    {|c| c[0]>64 ? (c[0]-55).to_s.split(//).map {|d| d.to_i} : c.to_i } \
    .flatten.each_with_index \
    {|o,i| sum+= i%2!=0 ? (o*2).modulo(9)+(o/9)*9 : o}
  return (10-sum%10)%10

>> calc_ISIN_checksum("US38259P508")
=> 3