Created
September 27, 2013 15:16
-
-
Save mecampbellsoup/6730231 to your computer and use it in GitHub Desktop.
Phone # formatter!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'awesome_print' | |
| require 'pry' | |
| def normalize_phone_number number_string | |
| numbers_only = [] | |
| if number_string.size >= 10 | |
| number_string.gsub!(/\D/, '') | |
| number_string.each_char do |char| | |
| if char.to_i.is_a?(Numeric) | |
| numbers_only << char | |
| end | |
| end | |
| numbers_only.flatten! | |
| numbers_only = numbers_only.join | |
| fixed_phone_number = "(#{numbers_only.slice(0,3)}) #{numbers_only.slice(3,3)}-#{numbers_only.slice(6,4)}" | |
| else | |
| number_string | |
| end | |
| end | |
| ap normalize_phone_number "123 456 7890" | |
| ap normalize_phone_number "1234567890" | |
| ap normalize_phone_number "(123)4567890" | |
| ap normalize_phone_number "123ABF90" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment