Last active
August 29, 2015 14:22
-
-
Save KBeltz/0abed898a835525cb7bf to your computer and use it in GitHub Desktop.
Phone Number Formatter
This file contains 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_relative "phone_number_formatter.rb" | |
# gets input from user | |
puts "Please enter a phone number: " | |
phone_number = gets.chomp | |
# creates a new instance of the PhoneNumberFormatter class | |
phone_number = PhoneNumberFormatter.new(phone_number) | |
# output string, phone number with correct format | |
puts phone_number.format |
This file contains 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
class PhoneNumberFormatter | |
attr_accessor :phone_number | |
# initialize method | |
# | |
# phone_number - data type | |
# | |
# returns self | |
def initialize(phone_number) | |
@phone_number = phone_number | |
end | |
# formats phone number entered by user | |
# | |
# returns string | |
def format | |
if phone_number.length < 12 | |
phone_number.insert(3, "-") | |
phone_number.insert(7, "-") | |
end | |
end | |
end |
This file contains 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 "minitest/autorun" | |
require_relative "phone_number_formatter.rb" | |
class PhoneNumberFormatterTest < Minitest::Test | |
# when I run the format method, | |
# I expect it to change a phone number 1234567890 | |
# and return a phone number formatted 123-456-7890 | |
def test_formatting_phone_number | |
phone_number = PhoneNumberFormatter.new("1234567890") | |
assert_equal("123-456-7890", phone_number.format) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment