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 Ruby Booleans Build a Simple Todo List Program Part 4: Removing Todo Items

Ari Misha
Ari Misha
19,323 Points

Do ya think this might work?

I wasnt convinced with the "remove_item()" method Jason made in the video, so i tried to come up with a simple and not so complex solution for it. BTW , Im following along in RubyMine IDE. And i ran this code , it kinda worked. So my question is, do i need to modify/update it or this code is good enough? Thank You!

THIS IS todo_list.rb file btw.

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_items(name)
    todo_items.delete_at(todo_items.index(name).to_i)
  end

end

todo_list = TodoList.new('groceries')
todo_list.add_item('Milk')
todo_list.add_item('Eggs')
todo_list.add_item('Bread')

puts todo_list.inspect

if todo_list.remove_items('Milk')
  puts 'Milk was removed from the list.'
end
puts todo_list.inspect

1 Answer

James Nobles
James Nobles
8,884 Points

What do you mean by "kinda works"? Any errors or unexpected behaviors? I can't run the code myself right now.

I think the answer comes down to how ruby handles searching arrays/hashes. Jason's code resets things to 0 each time around runs each item, then passes the exact item to the delete function. Your code just hands a name to the delete function and says delete this. Does ruby automatically iterate through the index? If so then you code is quicker and Jason's code just tells the program to do something it would do already. He's just going into more details for educational purposes.