-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperson.rb
55 lines (40 loc) · 1.11 KB
/
person.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Person
attr_reader :first_name, :surname, :dob, :email, :phone_numbers
def initialize(fname, sname, dob = nil)
@first_name = fname.capitalize
@surname = sname.capitalize
@dob = Date.parse(dob) if dob
@email = []
@phone_numbers = []
end
def fullname
"#{@first_name} #{@surname}"
end
def add_email(add_email)
regex = /\A\w+(?:\.\w+)?@[a-z]+\.(?:com|org|co\.uk)\z/
check = regex.match(add_email)
if check == nil
raise ArgumentError, "Please enter a correct email address."
else
@email << add_email
end
end
def add_phone(add_phone)
regex = /020\d+{8}/
check = regex.match(add_phone)
if check == nil
raise ArgumentError, "Please enter a correct phone number."
else
@phone_numbers << add_phone
end
end
def remove_email(remove_email)
@email.delete_at(remove_email)
end
def remove_phone(remove_phone)
@phone_numbers.delete_at(remove_phone)
end
def person_s
return "#{fullname} was born on #{@dob}.\n Their email addresses are:#{@email}.\n Their phone numbers are:#{@phone_numbers}.\n"
end
end