Advertisement
Guest User

Untitled

a guest
May 25th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. def format_ssns(string)
  2. # get array of ssns without damaging string
  3. string_copy = string.dup
  4. array_of_ssns = grab_all_ssns(string_copy)
  5. # remove all non-numbers from ssn in the array
  6. array_of_ssns.map! do |ssn|
  7. ssn = ssn.gsub(/\D/, "")
  8. # insert dashes in the correct places
  9. ssn = ssn.insert(3, "-").insert(6, "-")
  10. end
  11. # loop through ssns in string and replace with index[0] of ssn array
  12. string_as_array = string.split(' ')
  13. string_as_array.map! do |word|
  14. if word =~ /\d{3}\D*\d{2}\D*\d{4}(.*)/
  15. additional = word.match(/\d{3}\D*\d{2}\D*\d{4}(.*)/)[1]
  16. ssn_plus_additional = array_of_ssns[0] + additional
  17. word = ssn_plus_additional
  18. array_of_ssns.delete_at(0)
  19. end
  20. word
  21. end
  22. string_as_array.join(' ')
  23. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement