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 trial

Ruby Build an Address Book in Ruby Input and Output Searching the Address Book

Undefined gsub Method

My error on undefined gsub method

Error Message

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

At 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.

Aimee Ault Ah got ya! ;D

In 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.