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 3: Adding Todo Items

Hunter Garrett
Hunter Garrett
6,339 Points

Why do we have to pass the class 'TodoItem' in the add_item method?

I wasn't clear why we had to do this instead of just passing 'name'

Is it because TodoItem is in another file?

1 Answer

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

We are being explicit, name could be referring to something else. But by passing in an instance of the TodoItem class, (with the name of the item passed in upon initialization), we are telling Ruby to append a new item to the todo_items array every time the add_item method is called with the name of the item we want to add (to our Todo List) passed in as an argument to that method.

In other words, it is because the TodoItem class takes name as an argument (which is the name of the item we want to add) therefore, when the expression

TodoItem.new(name) 

in

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

gets evaluated, it returns a new todo item, which gets sent to the push method which then appends it to the todo_items array