Skip to content

Instantly share code, notes, and snippets.

@bpinto
Created June 12, 2012 23:30
Show Gist options
  • Save bpinto/2920803 to your computer and use it in GitHub Desktop.
Save bpinto/2920803 to your computer and use it in GitHub Desktop.
Ruby CPF Generator
class CPF
def self.generate
digits = []
9.times { digits << rand(9) }
2.times { digits << self.verification_digit_for(digits) }
digits.join
end
private
def self.verification_digit_for(known_digits)
i = 1
sums = known_digits.reverse.collect do |digit|
i = i + 1
digit * i
end
verification_digit = 11 - sums.inject(0) { |sum, item| sum + item } % 11
verification_digit < 10 ? verification_digit : 0
end
end
puts CPF.generate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment