Skip to content

Instantly share code, notes, and snippets.

@tuupola
Created April 15, 2011 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuupola/921468 to your computer and use it in GitHub Desktop.
Save tuupola/921468 to your computer and use it in GitHub Desktop.
Validate Estonian national identification code with Ruby.
#
# Validate Estonian national identification code.
#
# Copyright (c) 2011 Dmitri Smirnov, Mika Tuupola
#
# Adapted from:
# http://www.dmitri.me/misc/isikukood/isikukood.rb
#
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license.php
#
def isikukood_is_valid?(code)
code = code.to_s
control = code[10].chr.to_i
2.times do |i|
sum = 0
j = 2 * i + 1
10.times do |c|
sum += j * code[c].chr.to_i
j = 9 == j ? 1 : j + 1;
end
return sum == control if ((sum %= 11) < 10)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment