Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

My extra credit solution( Ruby Address Book)

Hey guys, so I had a go at adding a delete method to the address book program.

I started by adding a new option('d') to the menu which prompts a user for the name of the contact they want to delete and passes it to a new method (search_and_destroy)

def run 
        loop do 
            puts "Address Book"
            puts "a: Add Contact"
            puts "d: Remove Contact"
            puts "s: Search"
            puts "p: Print Address Book"
            puts "e: Exit"
            print "Enter your choice: "
            input = gets.chomp.downcase
            case input
            when 'a'
                add_contact
            when 'd'
                print "Enter the name of the contact you wish to delete: "
                name = gets.chomp
                search_and_destroy(name)
            when 'p'
                print_contact_list
            when 's'
                print "Search term: "
                search = gets.chomp
                find_by_name(search)
                find_by_phone_number(search)
                find_by_address(search)
            when 'e'
                save()
                break
            end
            puts "\n"
        end
    end

This is the method for deleting the contact....

     def search_and_destroy(name)
        results = []
        search = name.downcase
        contacts.each do |contact|
            if contact.full_name.downcase.include?(search)
                results.push(contact)
            end
        end
        if results.size > 0
            puts "Your search returned #{results.size} records"
            results.each do |contact|
                puts contact.full_name
                print "Delete this contact? (y/n): "
                response = gets.chomp
                case response
                when 'y'
                    contacts.delete(contact)
                    puts "Deleted #{contact.full_name}"
                end
            end
        else
            puts "There are no records that match that name!"
        end
    end

As you can see it performs the standard search by name function we wrote earlier in the program, then I have added an if statement to account for there being no records matched to the user input.

Then we have a loop that goes through all the contacts in the results array and asks if you wish to delete them.

Voila!

Anybody got anything similar?

Ken