Skip to content

Instantly share code, notes, and snippets.

@samtalks
Created September 27, 2013 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samtalks/6729350 to your computer and use it in GitHub Desktop.
Save samtalks/6729350 to your computer and use it in GitHub Desktop.
Morning lab 9/27
# Download this file:
# https://gist.github.com/scottcreynolds/ac1b5c8d96de0c91bf7c/download
# Run it from your terminal with:
# ruby ruby_phone_format.rb
# (Just make sure you are in the right directory)
# ======================================
# Ignore All This Code
# ======================================
@tests = 0
def test(title, &b)
@tests += 1
begin
if b
result = b.call
if result.is_a?(Array)
puts "#{@tests}. fail: #{title}"
puts " expected #{result.first} to equal #{result.last}"
elsif result
puts "#{@tests}. pass: #{title}"
else
puts "#{@tests}. fail: #{title}"
end
else
puts "#{@tests}. pending: #{title}"
end
rescue => e
puts "fail: #{title}"
puts e
end
end
def assert(statement)
!!statement
end
def assert_equal(actual, expected)
if expected == actual
true
else
[expected, actual]
end
end
class Object
def __
puts "__ should be replaced with a value or expression to make the test pass."
false
end
end
# ======================================
# Start Here - Make these tests pass.
# ======================================
# Define a method named normalize_phone_number that takes one
# string argument and returns a string in the format
# (XXX) XXX-XXXX if possible, and just returns the input string if not
# Method definition goes here:
def normalize_phone_number(str)
arr = []
new_num = ""
str.split("").each do |char|
arr << char if (char =~ /[0-9]/) == 0 # 0 if num, nil if not
end
return str if arr.length != 10
arr.insert(0, "(")
arr.insert(4, ")", " ")
arr.insert(-5, "-")
new_num = arr.join("")
end
# 1.
test 'that "1234567890" gets formatted' do
assert_equal normalize_phone_number("1234567890"), "(123) 456-7890"
end
# 2.
test 'that "(123)4567890" gets formatted' do
assert_equal normalize_phone_number("(123)4567890"), "(123) 456-7890"
end
# 3.
test 'that "123 456 7890" gets formatted' do
assert_equal normalize_phone_number("123 456 7890"), "(123) 456-7890"
end
# 4.
test 'that "123-4567890" gets formatted' do
assert_equal normalize_phone_number("123-4567890"), "(123) 456-7890"
end
# 5.
test 'that "123-456-7890" gets formatted' do
assert_equal normalize_phone_number("123-456-7890"), "(123) 456-7890"
end
# 6.
test 'that "123ABF90" does not get formatted' do
assert_equal normalize_phone_number("123ABF90"), "123ABF90"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment