Write a phonenumber class that gets initialized with a


Write a PhoneNumber class that gets initialized with a 10-digit phone number. In the call PhoneNumber.new(ph), the input ph take any of these possible formats:

- a 10-digit integer
- a string of 10 digits
- (ddd) ddd-dddd where d is any digit [parenthesis format]
- ddd-ddd-dddd where d is any digit [hyphen format]

Wherever whitespace appears in a string format, we can have a string of zero or more whitespace characters (including before and after the string as a whole). A PhoneNumber object responds to the to_s message which returns the number as a string in parenthesis format, and the area_code, prefix, and root messages which return the parts of the number in string format, as illustrated here:

a = PhoneNumber.new(1234567890)
puts a # (123) 456-7890
puts a.area_code # 123
puts a.prefix # 456
puts a.root # 7890
puts PhoneNumber.new(' 7778889999 ') # (777) 888-9999
puts PhoneNumber.new(' (555)444-3333') # (555) 444-3333
PhoneNumber.new('1234')
# ArgumentError: Improper phone number syntax
PhoneNumber.new('(12) 333-4444')
# ArgumentError: Improper phone number syntax
PhoneNumber.new('123- 456- 7890')
# ArgumentError: Improper phone number syntax

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Write a phonenumber class that gets initialized with a
Reference No:- TGS0663244

Expected delivery within 24 Hours