Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. # Determine whether a string contains a SIN (Social Insurance Number).
  2. # A SIN is 9 digits and we are assuming that they must have dashes in them
  3.  
  4. def ssn
  5. /\b\d{3}-\d{3}-\d{3}\b/
  6. end
  7.  
  8. def has_sin?(string)
  9. ssn =~ string ? true : false
  10. end
  11.  
  12. puts "has_sin? returns true if it has what looks like a SIN"
  13. puts has_sin?("please don't share this: 234-604-142") == true
  14.  
  15. puts "has_sin? returns false if it doesn't have a SIN"
  16. puts has_sin?("please confirm your identity: XXX-XXX-142") == false
  17. puts has_sin?("please don't share this: 234-6043-142") == false
  18. puts has_sin?("please don't share this: 2342-604-142") == false
  19. puts has_sin?("please don't share this: 234-604-1421") == false
  20.  
  21.  
  22. # Return the Social Insurance Number from a string.
  23. def grab_sin(string)
  24. string[ssn]
  25. end
  26.  
  27. puts "grab_sin returns an SIN if the string has an SIN"
  28. puts grab_sin("please don't share this: 234-604-142") == "234-604-142"
  29.  
  30. puts "grab_sin returns nil if it doesn't have a SIN"
  31. puts grab_sin("please confirm your identity: XXX-XXX-142") == nil
  32.  
  33.  
  34. # Return all of the SINs from a string, not just one.
  35. def grab_all_sins(string)
  36. string.scan(ssn)
  37. end
  38.  
  39. puts "grab_all_sins returns all SINs if the string has any SINs"
  40. puts grab_all_sins("234-604-142, 350-802-074, 013-630-876") == ["234-604-142", "350-802-074", "013-630-876"]
  41.  
  42. puts "grab_all_sins returns an empty Array if it doesn't have any SINs"
  43. puts grab_all_sins("please confirm your identity: XXX-XXX-142") == []
  44.  
  45.  
  46. # Obfuscate all of the Social Insurance numbers in a string. Example: XXX-XX-4430.
  47. def hide_all_sins(string)
  48. string.gsub(/\b\d{3}-\d{3}\b/, "XXX-XXX")
  49. end
  50.  
  51. puts "hide_all_sins obfuscates any SINs in the string"
  52. puts hide_all_sins("234-601-142, 350-801-074, 013-601-876") == "XXX-XXX-142, XXX-XXX-074, XXX-XXX-876"
  53.  
  54. puts "hide_all_sins does not alter a string without SINs in it"
  55. string = "please confirm your identity: XXX-XXX-142"
  56. puts hide_all_sins(string) == string
  57.  
  58. # Ensure all of the Social Insurance numbers use dashes for delimiters.
  59. # Example: 480.01.4430 and 480014430 would both be 480-01-4430.
  60. def format_sins(string)
  61. string.gsub(/(\d{3})(-|.)?(\d{3})(-|.)?(\d{3})/, '\1-\3-\5')
  62. end
  63.  
  64. puts "format_sins finds and reformat any SINs in the string"
  65. puts format_sins("234600142, 350.800.074, 013-600-876") # == "234-600-142, 350-800-074, 013-600-876"
  66.  
  67. puts "format_sins does not alter a string without SINs in it"
  68. string = "please confirm your identity: 4421422"
  69. puts format_sins(string) == string
  70.  
  71. string = "please confirm your identity: 123abc445"
  72. puts format_sins(string) == string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement