Ruby Build an Address Book in Ruby Search Searching By Address

Jason Woo
Jason Woo
11,730 Points

Excluding variables in search?

The address search results include the 'kind' variable. Which means if I search 'm', I'm going to get all contacts with 'Home' addresses without 'm's. How should I exclude this?

Jason Woo
Jason Woo
11,730 Points
require "./contact"

class AddressBook
    attr_reader :contacts

    def initialize
        @contacts = []
    end

    def print_address_book
        puts "Contact List"
        puts "-"*40
        contacts.each do |contact|
            puts contact.to_s('last_first')
            contact.print_phone_numbers.to_s + "/n" +
            contact.print_addresses.to_s
            puts "-"*40
        end
    end 

    def print_search(search, results)
        puts search
        puts "\n"
        results.each do |contact|
            puts contact.to_s('full_name')
            puts "-"*40
            contact.print_phone_numbers
            contact.print_addresses
            puts "\n"
            end
        end

    def find_by_name(name)
        results_num = []
        search = name.downcase
        contacts.each do |contact|
            if  contact.full_name.downcase.include?(search)
                results_num.push(contact)
            end
        end
        print_search("Name Search Results for: #{search}", results_num)
    end

    def find_by_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_search("Name Search Results for: #{search}", results)
    end

    def find_by_address(address)
        results = []
        search = address.downcase
        contacts.each do |contact|
            contact.addresses.each do |address|
                if address.to_s('long').downcase.include?(search)
                    results.push(contact) unless results.include?(contact)
                end
            end
        end
        print_search("Address Search Results for: #{search} ", results)
    end
end

address_book = AddressBook.new

jason = Contact.new
jason.first_name = 'Jason'
jason.middle_name = 'Way'
jason.last_name = 'Woo'
jason.add_phone_number('Cell', '773-895-4068')
jason.add_phone_number('Home', '847-568-1581')
jason.add_address('Home', '9828 Keeler', "", 'Skokie', 'IL', '60076')


justin = Contact.new
justin.first_name = 'Justin'
justin.middle_name = ''
justin.last_name = 'Woo'
justin.add_phone_number('Cell', '623-111-3834')
justin.add_address('Home', '123 Main', "", 'New York', 'NY', '23145')



address_book.contacts.push(jason)
address_book.contacts.push(justin)


# address_book.find_by_name('j')
# puts "\n"
# address_book.find_by_number('8')
address_book.find_by_address('M')

1 Answer

Jeff Lange
Jeff Lange
8,788 Points

It isn't clear what you're asking. Can you clarify what you mean?

Matthew Wilkes
Matthew Wilkes
4,809 Points

He wants to exclude the 'Kind' of address from the results, so if you were to search for an address that contained 'home' it would only contain the addresses that had it as part of their address i.e. 123 home lane. Currently it would show every 'home' address.