Skip to content

Instantly share code, notes, and snippets.

@luizfonseca
Created July 11, 2017 15:59
Show Gist options
  • Save luizfonseca/ed1c5174984e66c0a9e535490721a921 to your computer and use it in GitHub Desktop.
Save luizfonseca/ed1c5174984e66c0a9e535490721a921 to your computer and use it in GitHub Desktop.
Le Wagon, March 10 2017
require "date"
# REGEX TIME!
# Write a #french_ssn_info method extracting infos
# from French SSN (Social Security Number) using
# regexp.
# "1 84 12 76 451 089 46"
# Gender (1 == man, 2 == woman)
# Year of birth (84)
# Month of birth (12)
# Department of birth (76, basically included between 01 and 99)
# 6 random digits (451 089)
# A 2 digits key (46, we'll see later how it can be deduced from the rest of the digits)
# french_ssn_info("1 84 12 76 451 089 46")
# => "a man, born in December, 1984 in Seine-Maritime."
puts "Input your French SSN Info!"
user_ssn = gets.chomp # User inputed something <<<
#"1 84 12 76 451 089 46"
def french_ssn_info(ssn)
rule = /^(?<gender>[1|2])\W+(?<year>\d{2})\W+(?<month>[0][1-9]|[1][0-2])\W+(?<dept>[0-9][1-9])\W+(?<random>\d{3}\W+\d{3})\W+(?<key>\d{2})$/
result = ssn.match(rule)
unless result
puts "Its not a valid SSN"
return
end
puts "a #{gender(result[:gender])}, born in #{month(result[:month])}, 19#{result[:year]} in Seine-Maritime."
end
def gender(input)
return input == "1" ? "man" : "woman"
end
def month(num)
return Date::MONTHNAMES[num.to_i]
end
french_ssn_info(user_ssn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment