Building a Phone Parser in Ruby
In this guide, you will walk through the steps to implement a method that will allow various users to input their phone numbers differently, but provides the system with one way to store the data.
Guide Tasks
  • Read Tutorial
  • Complete the Exercise

Summary

Build a method that validates phone numbers.

Exercise Description

Define a method that can parse any US based phone number in various formats and then informs the client if there are too few or too many digits. It should also allow a valid country code to be supplied and raise an error if an invalid country code is supplied.

Example Data

    expect(PhoneParser.parse('555-555-5555')).to eq('5555555555')
    expect(PhoneParser.parse('5555555555')).to eq('5555555555')
    expect(PhoneParser.parse('(555) 555-5555')).to eq('5555555555')
    expect(PhoneParser.parse('555.555.5555')).to eq('5555555555')
    expect(PhoneParser.parse('555 555.5555')).to eq('5555555555')
    expect(PhoneParser.parse('555 555.5555')).to eq('5555555555')

Real World Usage

This exercise is helpful to know if you are developing an application that requires users to provide their phone number. It simplifies the information so that there won't be multiple versions of numbers entered into the system.

Test Cases

Code File