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

Some methods of the Todo List program not functioning correctly.

Hi!

So I recently completed Ruby Booleans, awesome course by the way Jason Seifer, and I worked my way through the program with Jason however for some reason my remove_item and mark_complete methods don't seem to be functioning properly! I have had a scan through them and can't spot anything at the moment.

Is there anything you guys can see?

todo_list.rb

require "./todo_item"

class TodoList
    attr_reader :name, :todo_items
    def initialize(name)
        @name = name
        @todo_items = []
    end

    def add_item(name)
        todo_items.push(TodoItem.new(name))
    end

    def remove_item(name)
        if index = find_index(name)
            todo_items.delete_at(index)
            return true
        else
            return false
        end
    end

    def mark_complete(name)
        if index = find_index(name)
            todo_items[index].mark_complete!
            return true
        else
            return false
        end
    end

    def find_index(name)
        index = 0
        found = false

        todo_items.each do |todo_item|
            if todo_item.name == name
                found = true
            end
            if found
                break
            else
                index += 1
            end
            if found
                return index
            else
                return nil
            end
        end
    end

    def print_list(kind="all")
        puts "#{name} List"
        puts "-" * 30
        todo_items.each do |todo_item|
            case kind
                when 'all'
                    puts todo_item
                when "complete"
                    puts todo_item if todo_item.complete?
                when "incomplete"
                    puts todo_item unless todo_item.complete?
                end
            end
        end
        puts "\n"
    end

groceries = TodoList.new("Groceries.")
groceries.add_item("Butter")
groceries.add_item("Eggs")
if groceries.remove_item("Eggs")
  puts "Eggs were removed from the list."
end
groceries.print_list

todo_item.rb

class TodoItem
    attr_reader :name
    def initialize(name)
        @name = name
        @complete = false
    end

    def mark_complete!
        @complete = true
    end

    def complete?
        @complete
    end

    def mark_incomplete!
        @complete = false
    end

    def to_s
        if complete?
            "[C] #{name}"
        else
            "[I] #{name}"
        end
    end


end

I don't get any errors whilst I am running the program but the methods aren't doing what they are supposed to.

Also, whilst I am scanning through the code again I cannot remember what the "index" variable is doing in the function mark_complete and similar functions! I have had a look through the code and I cannot figure it out! If you guys could let me know that would be great. However, if you can't I am sure I will figure it out once I have got some rest!

Thank you ever so much to anyone who helps me out and I hope you guys are all having a great day!

-Luke

1 Answer

Brandon Barrette
Brandon Barrette
20,485 Points

The error is so slight it's crazy. It's happening in your find_index method.

def find_index(name)
        index = 0
        found = false

        todo_items.each do |todo_item|
            if todo_item.name == name
                found = true
            end
            if found
                break
            else
                index += 1
            end
            if found
                return index
            else
                return nil
            end
        end
    end

Notice that you have the last if found statement inside the todo_items loop. You actually want to have it outside the loop, but inside the method. You just have to move an end:

def find_index(name)
        index = 0
        found = false

        todo_items.each do |todo_item|
            if todo_item.name == name
                found = true
            end
            if found
                break
            else
                index += 1
            end
         end
         # this needs to be outside the loop   
         if found
                return index
           else
                return nil
            end
    end

I didn't realize it until I ran your code locally on my computer. It's so slight and a common little error that can cause many headaches! Happy coding!

Thank you so much for this! I spent a long time looking around for this error and I have managed to get it fixed now. Thank you ever so much!