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

Jorge Rodriguez
Jorge Rodriguez
2,616 Points

Which Boolean is loaded ?

In this part of the code, which is the boolean inside the flag found?

 if found < ----HERE
     todos_items.delete_at(index)
     return true
   else
     return false

What i'm seeing it should be "false" but it is working with "true", why is that? It doesn't take the flag at this point(false)?

Regards,

Jorge

2 Answers

Jason Milfred
Jason Milfred
5,968 Points
if found

is just short for

if found == true

That's just a conditional statement. The value of found at this point will depend on wether or not the name provided as an argument in the delete method matches an item in the todo_items array. Found is initially set to false, but if there is a match, it will be set to true.

found = false # found is set to false
todo_items.each do |todo_item| # iterate through each item in the todo_items array
    if todo_item.name == name # check to see if the name provided matches the first item in the array
        found = true # if it does, set the value of found to true
    end
    if found # if the value of found is true
        break # stop iterating through the array and move on
    else
        index += 1 # otherwise, increase the value of index by one
    end # then the each method will do all of this again for the next item in the array
end

Then the if statement you are referencing is run. The value of found could be true or false it this point.

Jorge Rodriguez
Jorge Rodriguez
2,616 Points

Thank you!, it's very clear now!