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 trialKhairul Akmal
Courses Plus Student 16,527 PointsUndefined gsub Method
My error on undefined gsub method
Here's the code:
# Find by phone number in address book and print all searches
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
3 Answers
Aimee Ault
29,193 PointsAt a quick glance, could it be that you're trying to perform gsub on phone_number.number
instead of just phone_number
? The error you're getting is because it's not finding number
on phone_number
.
Sergio Niño
Full Stack JavaScript Techdegree Student 22,976 PointsSo what I have to do? :)
Andrew Phythian
19,747 PointsIn order to avoid the "gsub" error described above, it's important to check the 'add_contact' method, specifically the case response block (that comes directly after the response = gets.chomp line).
It must read
phone.number = gets.chomp
and assuming it does, you should avoid that error.
Khairul Akmal
Courses Plus Student 16,527 PointsKhairul Akmal
Courses Plus Student 16,527 PointsAimee Ault Ah got ya! ;D