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

Aimee Ault
Aimee Ault
29,193 Points

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

Andrew Phythian
Andrew Phythian
19,746 Points

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.