Question: USE RUBY PROGRAMMING LANGUAGE: Write a PhoneNumber class that gets initialized with a 10-digit phone number. In the call PhoneNumber.new(ph), the input ph take any

USE RUBY PROGRAMMING LANGUAGE:

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!