Generate and validate Chilean RUTs with this script. Method checker_digit taken from wikipedia.

# Usage:
# - Get checker digit of a number
# rut.rb -V 15000000
# - Generate RUT numbers:
# rut.rb 15000000 20000000

def checker_digit(rut)
  total = rut.to_s.rjust(8,'0')
                  .split(//)
                  .zip(%w(3 2 7 6 5 4 3 2))
                  .collect{|a,b| a.to_i*b.to_i}.inject(:+)
  (11 - total % 11).to_s.gsub(/10/,'k').gsub(/11/,'0')
end

def rut(num)
  "#{num}-#{checker_digit(num)}"
end

if ARGV.first == '-V'
  puts rut(ARGV.last)
else
  ARGV[0].upto(ARGV[1]) do |num|
    puts rut(num)
  end
end


blog comments powered by Disqus

Published

08 June 2014