Utilizing the Phone Gem to Create, Format, and Query Phone Numbers


Working with phone numbers, especially for International applications, can be a challenging task. In this guide we’re going to walk through how to implement the popular Phone RubyGem in order to create, format, and work with phone numbers in a Ruby program.


Implement the Phone RubyGem in order to work with phone numbers.

Exercise File

Code File

Exercise Description

Work with the Phone RubyGem code library in order to build four methods. The methods should be able to properly:

  • Build a phone number object.
  • Query a phone number for separate components, such as area code and extension.
  • Format a phone number object to match the style (123) 456-7890.
  • Check to see if a phone number is valid.

Four Examples

phone = phone_builder(number: '1234567',
                          area_code: '480',
                          country_code: '1',
                          extension: '333')
phone.area_code # => '480'

phone_parser('+16021234567').area_code # => '602'

phone_formatter '+16021234567' # => '(602) 123-4567'

phone_valid? '+16021234567' # => true

Research the Phone gem. Each of these method calls can be built by simply calling modules and methods from the gem.

Real World Usage

Working with phone numbers can be incredibly complex when you consider all of the edge cases associated with tasks such as: extracting area codes for International numbers, working with dynamically sized phone extensions, etc. I have received multiple questions from students asking for the best way to work with phone numbers in Ruby and so I wanted to build a guide completely dedicated to the topic.


Can be found on the solutions branch on github.


Please enter your comment!
Please enter your name here