Created
April 15, 2011 09:42
-
-
Save tuupola/921468 to your computer and use it in GitHub Desktop.
Validate Estonian national identification code with Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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