Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkabir k
Courses Plus Student 18,036 Pointsphone_number vs. number
In the find_by_phone_number(number) method below, I know phone_number is an item in the phone_numbers array, but how is it different from the attribute accessor number
in the PhoneNumber class? Are they the same?
Also, how is number
different from phone_number in the inner each
iteration of the find_by_phone_number(number) method?
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
For instance, in the inner loop, why is it?
if phone_number.number.gsub("-", "").include?(search)
# line of code
end
and not something like this (without number):
if phone_number.gsub("-", "").include?(search)
# line of code
end
1 Answer
Charles Lee
17,825 PointsGreat question. One of the main reasons is that phone_number
is actually an instance of the class PhoneNumber. If you look into the workspaces you can see that each phone number has two attributes, kind
and number
.
In each iteration, we're simply pulling out checking only the number.
PhoneNumber class from the video.
class PhoneNumber
attr_accessor :kind, :number
def to_s
"#{kind}: #{number}"
end
end
kabir k
Courses Plus Student 18,036 Pointskabir k
Courses Plus Student 18,036 PointsThanks, Charles.