Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Build an Address Book in Ruby!
You have completed Build an Address Book in Ruby!
Preview
In this video, we implement a method to search for contacts by their name.
Code Samples
Here is our find_by_name method:
def find_by_name(name)
results = []
search = name.downcase
contacts.each do |contact|
if contact.full_name.downcase.include?(search)
results.push(contact)
end
end
puts "Name search results (#{search})"
results.each do |contact|
puts contact.to_s('full_name')
contact.print_phone_numbers
contact.print_addresses
puts "\n"
end
end
Which we can call as follows:
address_book.find_by_name("e")
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
a list doesn't do us much good if we
don't have the ability to search through.
0:00
We're going to use some methods that take
blocks to search through our different
0:04
contacts.
0:08
Let's go ahead and try adding that
ability now using work spaces.
0:09
All right.
0:14
So, first up let's go ahead and
add the ability to find a contact by name.
0:15
We can print out the contact list.
0:21
But, we want to be able
to search through it.
0:23
So let's go ahead and write a method.
0:28
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