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

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