This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In our final bit of search functionality, we add the ability to search by address.
Code Samples
def find_by_address(query)
results = []
search = query.downcase
contacts.each do |contact|
contact.addresses.each do |address|
if address.to_s('long').downcase.include?(search)
results.push(contact) unless results.include?(contact)
end
end
end
print_results("Address search results (#{search})", results)
end
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
Okay, so
we have address book looking pretty good.
0:00
We can find people by name and
we can find people by phone number.
0:04
Finally, let's go ahead and add the
ability to find somebody by their address.
0:08
Now, this is going to follow a similar
pattern as finding by name and
0:14
phone number, except this time we're
going to be querying by the address.
0:19
So, let's go ahead and
create a method to find by an address.
0:23
Now if we scroll up,
0:33
we can see we have this pattern of
creating an empty results array and
0:34
then manipulating the search to get sent
in, and then looping through the contacts,
0:38
and appending to the array, unless
the contact is included in that array.
0:43
So let's go ahead and follow that
same pattern now, and this time,
0:48
what we're going to send in is a query,
because we're going to be looping and
0:53
finding the address, but
we'll see that in just a second.
0:58
So, we'll set up our empty results array,
and then we'll create our search variable,
1:02
which is just going to be
a lowercase version of the query.
1:10
Now we can loop through our contacts.
1:15
And then we're going to have to
loop through our addresses too.
1:22
Now let's go ahead and
look at the address class for a moment.
1:26
We have this to_s method, and we
learned before that we can use the include
1:29
method to see if one string
is included in another.
1:34
So we're gonna use the long form
of the address to see whether or
1:39
not the search is included in there.
1:43
But we need to loop
through each address for
1:46
each contact to see if
the query is contained there.
1:51
So let's go ahead and do that.
1:56
We're in a contact right now, so we'll
go ahead and loop through the addresses.
1:57
Now we can say if the long form
of the address downcase that,
2:08
and then we'll see if
that includes the search.
2:14
Then we can follow the same
pattern as before.
2:21
And append the contact to the results,
2:26
unless the results already
have that contact.
2:30
So, we don't have any duplicates
when we print it out.
2:35
And then finally, we can use that print
results method to display the results.
2:39
So now, all we have to do is,
we can scroll down here.
2:54
And let's just search for two, since we
know that Nick has an address on Two Lane.
3:04
And comment those out just to
clean up the output a little bit.
3:14
And then go down in the console here.
3:19
If I type ruby address_book.rb,
3:21
we can see that we have Nick turning
up in the address search results for
3:26
two, which is exactly what we wanted.
3:31
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