Skip to content

Instantly share code, notes, and snippets.

@sergiocampama
Created June 21, 2013 14:27
Show Gist options
  • Save sergiocampama/5831534 to your computer and use it in GitHub Desktop.
Save sergiocampama/5831534 to your computer and use it in GitHub Desktop.
Ruby class extensions for Chilean RUTs
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