Question: I need a way to add the '-' into a phone number, verify that it is the correct length, add parenthesis around the area code, and default to nil if no extension is given. For example:

```
9876542123 ext: 6789 -> Should end up -> (987)654-2123 ext: 6789
4543456 -> should fail
9876785678 ext: -> should end up -> (987)678-5678

```

How can I do this without a regular expression?

What I've attempted:

```
def format_phone_number(num)
  num.insert(0, '(').insert(-3, ')').insert(-4, '-')
end

```

Outputs:

```
(12345678-)94

```

Answer: 
The following should closely resemble what you're looking for. When you insert a character into a string then you have to remember that you're increasing the length of the string by 1. Also, using a negative index with insert, inserts the character at position `num.length - index`.

```
def format_phone_number(num)
  if num.length >= 10
    num.insert(0, '(').insert(4, ')').insert(8, '-')
  end
end

```

I'm not sure what you want to return if the phone number is not the appropriate length.
I don't really understand why, but this worked:

```
def format_phone_number(num)
  num.insert(0, '(').insert(4, ')').insert(8, '-')
end

```

When I input:

```
1234567894 -> (123)456-7894

```