Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Part 3: Adding Todo Items2:42 with Jason Seifer
TodoItem class set up, we can go about adding those items in our
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 end todo_list = TodoList.new("Groceries") todo_list.add_item("Milk") todo_list.add_item("Eggs") puts todo_list.inspect
Okay so here is our to-do list class, and we have modified to do item,
that looks pretty good, does pretty much everything that we want it to do.
So let's go ahead and add a method for adding to-do items to our to-do list.
This is going to be a pretty easy method to write.
Let's go ahead and call it add_ item.
And what are we going to add to this list?
Let's just give it a name.
And so what we will do here is append to the todo items array.
Now if we scroll back up we can see that todo items is
an attribute reader so we don't have to use the at symbol to access it.
What we'll do here is say todoitems.push.
And you might think it's just name, but
what we want to do is make this an actual instance of the to do item class.
So we can it and
pass the name as an argument.
So what will happen here Is we have this add item method.
It takes the argument of name, and then we are going to instantiate
a new to do item with name as the argument.
We can see that out todo item class takes name as an argument.
So here we go, todoItem.new.
Now this is in parentheses, so the result of this expression
will be evaluated in here as its own expression, returning a new todoItem.
From there, that todoItem gets sent to the push method,
which then appends to the todo_items array.
Now let's go ahead and take all this out here.
And say to do list.add_item milk, and then let's just inspect the to do list.
we print this out here, and this looks pretty good.
We've got our TodoList, and it's got the name and it's got a TodoItem in here.
Let's go ahead and add another item.
And just make sure it gets appended.
And it does, that looks pretty good.
In our next video, we're going to remove to do items.
You need to sign up for Treehouse in order to download course files.Sign up