-
Notifications
You must be signed in to change notification settings - Fork 0
/
name.rb
34 lines (29 loc) · 814 Bytes
/
name.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
class Name
# Define default getter methods, but not setter methods.
attr_reader :first, :last
# When someone tries to set a first name, enforce rules about it.
def first=(first)
if first.nil? || first.empty?
raise ArgumentError, 'Everyone must have a first name.'
end
# first = first.dup
first[0] = first[0].chr.capitalize
@first = first
end
# When someone tries to set a last name, enforce rules about it.
def last=(last)
if last.nil? || last.empty?
raise ArgumentError, 'Everyone must have a last name.'
end
@last = last
end
def full_name
"#{@first} #{@last}"
end
# Delegate to the setter methods instead of setting the instance
# variables directly.
def initialize(first, last)
self.first = first
self.last = last
end
end