Created
June 21, 2013 14:27
-
-
Save sergiocampama/5831534 to your computer and use it in GitHub Desktop.
Ruby class extensions for Chilean RUTs
This file contains 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
class Fixnum | |
def to_rut | |
digits = self.to_s.to_rut | |
end | |
end | |
class String | |
def to_rut | |
count = -1 | |
sum = self.each_char.to_a.map(&:to_i).reverse. | |
inject(0){|s, i| s + i * (((count += 1) % 6) + 2)} | |
verifier = ((11 - (sum % 11)) % 11) | |
verifier = "K" if verifier == 10 | |
self.to_s + "-" + verifier.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment