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
sbpn
Code Samples
def find_by_phone_number(number)
results = []
search = number.gsub("-", "")
contacts.each do |contact|
contact.phone_numbers.each do |phone_number|
if phone_number.number.gsub("-", "").include?(search)
results.push(contact) unless results.include?(contact)
end
end
end
print_results("Phone search results (#{search})", results)
end
def print_results(search, results)
puts search
results.each do |contact|
puts contact.to_s('full_name')
contact.print_phone_numbers
contact.print_addresses
puts "\n"
end
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, we have finding
a contact by name working.
0:00
Now let's go ahead and
implement finding by a phone number.
0:05
And this is gonna be relatively similar.
0:08
We can start by creating
an empty results array and
0:20
when we do that this variable is
gonna be local to just this method.
0:23
So no other methods are gonna have
access to it unless we pass it.
0:28
And now we're gonna be searching so
we'll be searching by whatever is sent in,
0:34
in this case it's a number
like a phone number.
0:38
And to make this search a little bit
better what we're gonna do is replace any
0:42
dashes in the string with nothing.
0:46
And we do that using the G sub method.
0:51
So we replace a dash with nothing and
that gets assigned to a new variable.
0:54
So if we were to send in something
like 111-111 that would go
1:01
into the search as the string 111111.
1:06
So now we can do the same
thing that we did above,
1:09
we can loop through the contacts.
1:12
And now we have to loop through
the contact phone numbers this time,
1:19
to see if the phone number is included.
1:22
So we look through the phone number here.
1:25
And now just like in the find name method
1:31
where we had to down case
it before we found it.
1:35
We're looking for the same thing, so
1:38
this time we have to manipulate
it the same way here.
1:41
So we'll say if
the phone_number.number that when
1:46
we replace the dashes with
nothing Include our search term.
1:52
Then we can append this
contact to the results array.
2:00
And now all we have to do
is print out the results.
2:06
And I can just copy this.
2:11
And paste it.
2:15
And this time I just say phone search
results instead of name search results.
2:22
So now we can comment this out and
we'll say address
2:29
book, find by phone number.
2:34
Let's just see if this works by doing 222.
2:42
So when we run this, it should print
out Nick's contact information.
2:45
Okay, and there we go.
2:51
Nick's contact info is
correctly printed out.
2:53
Now if we search for just the number two,
2:58
since both of us have
that as a phone number.
3:02
Clear my screen, we should get
both myself and Nick returned.
3:07
And we do.
3:14
So the last thing that we need to do here,
3:17
is you'll notice that we
are repeating the same logic here.
3:20
We're printing out the search results.
3:24
Along with a little label for the search.
3:27
We're doing this in both the find by
name and find by phone number methods.
3:30
So let's go ahead and
create a method called print results,
3:37
Which will take a label for the search.
3:45
And an array of results.
3:50
And what this is gonna do
is de-duplicate our code.
3:56
So, if we ever wanna change it we
only have to change it in one spot
3:59
that we'll call from
the individual methods.
4:03
So, we'll print out the search.
4:07
And since this is the same
in each of these.
4:10
I'm gonna cut that from there
paste it into this method.
4:14
And now I can say print_results.
4:25
And that will have the label,
and then this results array.
4:31
And then I can do the same thing here.
4:41
So now we're going to clear the screen.
4:54
And we can print this both times here.
4:59
Now we should see two different
searches when we print this out.
5:07
Oh that's hard to see,
let's do this one more time.
5:15
Okay, phone search results two.
5:19
Would help if I saved the file.
5:26
Now scroll back up here.
5:32
Name search results for E,
Jason Seifer, Nick Pettit.
5:35
Phone search results two.
5:40
We have Jason Seifer,
Jason Seifer, and Nick Pettit.
5:42
Why are I appearing in here twice?
5:47
Well that is because my phone
number is included twice.
5:51
You'll see that we loop
through the phone numbers and
5:57
two of my phone numbers
have the number two.
6:00
So if we wanted to we could say
append to the results.contact,
6:06
unless that contact is already included.
6:14
So I'm gonna clear the screen and
run this address book one more time.
6:19
And you'll notice that that perfectly
deduplicates the search results.
6:24
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