Skip to content

Instantly share code, notes, and snippets.

@juliends
Created January 15, 2018 17:42
Show Gist options
  • Save juliends/ed2ed50cf88a8d9b4ea086320ddc3903 to your computer and use it in GitHub Desktop.
Save juliends/ed2ed50cf88a8d9b4ea086320ddc3903 to your computer and use it in GitHub Desktop.
# we use the match method to extract the data from original string
# find the gender
# find the birth year
# find the birth month
# find the dept
# validate the number
require 'date'
DEPT = {
75 => "Paris",
76 => "Seine-Maritime"
}
SSN_REG = /^(?<gender>1|2)(?<year>\d{2})(?<month>\d{2})(?<department>\d{2})(\d{6})(?<key>\d{2})$/
def ssn_info(ssn)
#Return => "a man, born in March, 1986 in Paris."
ssn.gsub!(" ",'')
data = ssn.match(SSN_REG)
gender = data[:gender].to_i == 1 ? "man" : "woman"
year = data[:year].to_i
month = Date::MONTHNAMES[data[:month].to_i]
dept = DEPT[data[:department].to_i]
key = data[:key].to_i
number = ssn[0..12].to_i
expected_key = 97 - (number % 97)
if key == expected_key
puts "A #{gender}, born in #{month}, 19#{year} in #{dept}"
else
puts "Invalid key number"
end
end
puts ssn_info("186037510803191")
puts ssn_info("286107511451175")
puts ssn_info("1 84 12 76 451 089 46")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment