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
[MUSIC]
0:00
We have all of our contacts successfully
in the program but just printing out
0:04
a list doesn't do us much good if we
don't have the ability to search through.
0:08
We're going to use some methods that take
blocks to search through our different
0:13
contacts.
0:17
Let's go ahead and try adding that
ability now using work spaces.
0:18
All right.
0:23
So, first up let's go ahead and
add the ability to find a contact by name.
0:24
We can print out the contact list.
0:30
But, we want to be able
to search through it.
0:32
So let's go ahead and write a method.
0:37
Called find_by_name.
0:40
And we'll pass in a name.
0:45
Now, let's think about
how we're gonna do this.
0:49
What we can do is iterate over
each contact in the contact list.
0:52
And then, if the name matches the argument
to this method we'll go ahead and
0:58
return that contact.
1:03
So how are we gonna do that?
1:05
Well, we can start by
having an empty array
1:07
of search results which will be
the contacts in our contacts list.
1:11
And then we'll say the search is
the name which we send into the method.
1:19
Same thing here and here.
1:26
But now let's think about this for
just a moment.
1:29
If somebody's searching for part of
a name or they've capitalized the name,
1:32
we still want the search to
return the correct thing.
1:37
So what weβre going to do is
make the search query lowercase.
1:40
And then when weβre looking through the
different contacts weβll also match that
1:46
against the lowercase version of
the first, middle or last name.
1:51
So let's go ahead and
iterate through the contacts.
1:56
And then if it matches, we'll append
that contact to the results array.
2:00
So, here's a loop, and now we'll say,
2:11
contact.first_name, and
remember, we'll downcase this.
2:15
So now we're dealing with a lower
case version of the first name, and
2:20
strings have a method called include,
2:24
which lets you see if a string
includes another string.
2:26
So we can say if this includes our search,
2:32
we can append this contact
to the result array.
2:38
Now, what we can do is go through and
print out the results.
2:44
And then, just to be a little bit more
clear, we'll put in the search term.
2:52
Now we can iterate over the results.
2:59
Remember, this is going to be a contact.
3:04
Now we can print out the contact's name.
3:09
We'll print out their full name.
3:11
And we'll go ahead and
print their phone numbers, and
3:19
we'll go ahead and print the addresses,
and we'll also print a new line.
3:24
So now let's go down here, and
3:31
instead of printing out the contact list,
we can comment that out.
3:33
And let's first try searching for
Nick in all lower case and
3:43
when we run this we should hopefully see
Nick's contact information printed out.
3:47
Oh, undefined local variable or
method, seach.
3:56
That would appear to be a typo.
4:00
Here we go.
4:06
That's on line 18, that all looks good.
4:08
Let's run this again.
4:13
Okay, this looks good,
that's what we wanted.
4:16
Now, let's go ahead and
change this to an n, since both Nick and
4:22
I have that letter in our names.
4:26
And then we should hopefully see Jason and
Nick here.
4:29
And we do.
That looks good.
4:33
Let's go ahead and
change this to search for the letter e.
4:39
And that returned no search results.
4:47
Even though both of our last
names include the letter e.
4:51
So let's go ahead and add an or statement.
5:00
And we'll say if contact.last_name.
5:03
Includes the search,
we can push that to the search results.
5:11
Let's go ahead and run this again.
5:21
And, hey, we get the exact same
results that we were expecting.
5:23
And actually we could include the full
name here, instead of searching for
5:30
the first and last names, we can get
the middle name in there, as well.
5:36
Run that one more time.
5:42
All right,
now we have a name search working.
5:43
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