Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Contact Class: Part 1 6:05
- Create a Contact Class 1 objective
- Contact Class: Part 2 4:04
- Write a Method 1 objective
- Phone Number Class 6:15
- Write a to_s Method 1 objective
- Address Class: Part 1 5:00
- Address Class: Part 2 3:44
- Initializing and Calling Methods 2 objectives
- Address Book Class 4:25
- Instance variable instantiation 2 objectives

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
p2
Code Samples
class Contact
attr_writer :first_name, :middle_name, :last_name
def first_name
@first_name
end
def middle_name
@middle_name
end
def last_name
@last_name
end
def first_last
first_name + " " + last_name
end
def last_first
last_first = last_name
last_first += ", "
last_first += first_name
if !@middle_name.nil?
last_first += " "
last_first += middle_name.slice(0, 1)
last_first += "."
end
last_first
end
def full_name
full_name = first_name
if !@middle_name.nil?
full_name += " "
full_name += middle_name
end
full_name += ' '
full_name += last_name
full_name
end
def to_s(format = 'full_name')
case format
when 'full_name'
full_name
when 'last_first'
last_first
when 'first'
first_name
when 'last'
last_name
else
first_last
end
end
end
jason = Contact.new
jason.first_name = "Jason"
jason.last_name = "Seifer"
puts jason.to_s
puts jason.to_s('full_name')
puts jason.to_s('last_first')
nick = Contact.new
nick.first_name = "Nick"
nick.middle_name = "A"
nick.last_name = "Pettit"
puts nick.to_s('first_last')
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Nafeez Quraishi
12,416 Points2 Answers
-
Alphonse Cuccurullo
2,513 Points0 Answers
-
Nick Vitsinsky
7,246 Points1 Answer
-
Sean Flanagan
33,236 Points1 Answer
-
Sean Flanagan
33,236 Points2 Answers
-
PLUS
kabir k
Courses Plus Student 18,036 Points2 Answers
-
Michael Newman
39,508 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up