Clean up user-entered phone numbers so that they can be sent SMS messages.
The North American Numbering Plan (NANP) is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: 1
.
NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as area code, followed by a seven-digit local number. The first three digits of the local number represent the exchange code, followed by the unique four-digit number which is the subscriber number.
The format is usually represented as
(NXX)-NXX-XXXX
where N
is any digit from 2 through 9 and X
is any digit from 0 through 9.
Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.
For example, the inputs
+1 (613)-995-0253
613-995-0253
1 613 995 0253
613.995.0253
should all produce the output
6139950253
Note: As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.
For installation and learning resources, refer to the exercism help page.
For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:
gem install minitest
If you would like color output, you can require 'minitest/pride'
in
the test file, or note the alternative instruction, below, for running
the test file.
Run the tests from the exercise directory using the following command:
ruby phone_number_test.rb
To include color from the command line:
ruby -r minitest/pride phone_number_test.rb
Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
require 'minitest/autorun'
require_relative 'phone_number'
# Common test data version: 1.2.0 39cba0d
class PhoneNumberTest < Minitest::Test
def test_cleans_the_number
# skip
assert_equal "2234567890", PhoneNumber.clean("(223) 456-7890")
end
def test_cleans_numbers_with_dots
skip
assert_equal "2234567890", PhoneNumber.clean("223.456.7890")
end
def test_cleans_numbers_with_multiple_spaces
skip
assert_equal "2234567890", PhoneNumber.clean("223 456 7890 ")
end
def test_invalid_when_9_digits
skip
assert_nil PhoneNumber.clean("123456789")
end
def test_invalid_when_11_digits_does_not_start_with_a_1
skip
assert_nil PhoneNumber.clean("22234567890")
end
def test_valid_when_11_digits_and_starting_with_1
skip
assert_equal "2234567890", PhoneNumber.clean("12234567890")
end
def test_valid_when_11_digits_and_starting_with_1_even_with_punctuation
skip
assert_equal "2234567890", PhoneNumber.clean("+1 (223) 456-7890")
end
def test_invalid_when_more_than_11_digits
skip
assert_nil PhoneNumber.clean("321234567890")
end
def test_invalid_with_letters
skip
assert_nil PhoneNumber.clean("123-abc-7890")
end
def test_invalid_with_punctuations
skip
assert_nil PhoneNumber.clean("123-@:!-7890")
end
def test_invalid_if_area_code_does_not_start_with_2_9
skip
assert_nil PhoneNumber.clean("(123) 456-7890")
end
def test_invalid_if_exchange_code_does_not_start_with_2_9
skip
assert_nil PhoneNumber.clean("(223) 056-7890")
end
# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
def test_bookkeeping
skip
assert_equal 2, BookKeeping::VERSION
end
end
class PhoneNumber
def initialize(string)
@number = self.class.valid_phone_number?(string) ? self.class.sanitize(string) : 0
end
def number
"#{format('%010d', @number)}"
end
def area_code
number[0..2]
end
def core_number
number[3..9]
end
def to_s
"(#{ area_code }) #{ core_number[0..2] }-#{ core_number[3..6] }"
end
class << self
def valid_phone_number?(string)
regexp.match(string)
end
def sanitize(phone_number)
with_digits_only = remove_non_digits(phone_number)
trim_country_code(with_digits_only)
end
private
# Matches phone numbers from the USA, examples:
# 5556667777
# (555) 666-7777
# +1 555 666 7777
# (+1.555).666.7777
def regexp
delimiter = '[\.\-\s]?'
prefix = '\(?\+?1?' + delimiter + '\d{3}\)?'
Regexp.new('\A' + prefix + delimiter + '\d{3}' + delimiter + '\d{4}\z')
end
def remove_non_digits(phone_number)
phone_number.gsub(/\D/, '')
end
def trim_country_code(phone_number)
length_with_country_code = 11
starting_index = phone_number.length == length_with_country_code ? 1 : 0
phone_number[starting_index..-1]
end
end
end
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments
When I first wrote the method to_s like so def to_s "(#{ area_code }) #{ number[0..2] }-#{ number[3..6] }" end
I felt like I was mixing two levels of abstraction, so to make things even I introduced the core_number method.
I really wanted the sanitize method to be private, because I don't feel that it should be a part of the public interface, but, as I found out, in Ruby you can't call private class methods from instance methods (which makes perfect sense when you know how Ruby works, but still came to me as a surprise). I didn't want to implement this method as a private instance method since it has nothing to do with the object state, but maybe I should in this situation? I read in Practical OO Design by Sandi Metz about a convention to leave all methods public and single out those that are not part of the public interface by prefixing their names with "_", but I'm not sure if I should start using that convention, it doesn't seem popular.
BTW. I'm not exactly sure if the optional "+1" should be inside ((+1 555) 555 5555) or outside (+1 (555) 555 5555) the optional parentheses.
i like your use of class methods. i had a similar approach, but without class methods.