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 trialFabricio Portillo
16,718 Pointschallenge build a simple todo list program 12/12
In the TodoList class, fill in the contains? method so that it returns a boolean value if the todo items array contains an item with the name argument.
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 contains?(name)
end
def find_index(name)
index = 0
found = false
todo_items.each do |item|
found = true if item.name == name
break if found
index += 1
end
if found
return index
else
return nil
end
end
end
6 Answers
Gaylen Miller
19,286 PointsIt seemed like the obvious answer was to use the existing function
def contains?(name)
find_index(name) >= 0
end
But that didn't work.
Neither did the first answer using the array method "include?"
So I tried stealing the find_index code that passed
def contains?(name)
index = 0
found = false
todo_items.each do |item|
found = true if item.name == name
break if found
index += 1
end
found
end
William Li
Courses Plus Student 26,868 PointsSorry about that, Gaylen, I didn't pay attention to the fact that the add_item
instance method is adding a new object each time it gets called.
Yeah, your solution is correct, you can write it more concisely like this
def contains?(name)
@todo_items.each {|item| return true if item.name==name}
false
end
Sabri Helal
6,217 Pointscould be a stupid question but just wondering... I know your if statement doesn't require an end because it is on one line but i'm assuming your .each statement doesn't either because you used the curly brackets instead of do and end, am i correct?
William Li
Courses Plus Student 26,868 Pointsdef contains?(name)
@todo_items.include?(name)
end
That'd be my answer based on the code your provided.
hiroshi
13,701 PointsThis was my solution too (minus the @ sign since there is an attr_reader) but it keeps saying contains? does not return a boolean. I ran it through irb though and it does >.<
Yuriy Kristian
9,471 PointsStruggled with the same code, it just wouldn't work. I think there is too much copying in the challenges. Actually we had to just copy and paste the same code we wrote in the previous video. It's not actually challenging :)
Lee Parham
3,173 PointsIt was mine, but the test failed. I'm not sure if there's a nil value being passed somewhere. Thanks for your one line posted up-thread.
jason phillips
18,131 Pointsyou can also do this:
def contains?(name)
!find_index(name).nil?
end
Ela Myslimi
3,256 PointsIf you follow along the course they are referencing what they did in the remove_item method. If you write your code without completing the delete_at process it passes the contains? requirement.
def contains?(name)
if index = find_index(name)
return true
else
return false
end
end
Pedro Cabezas
9,646 Pointsdef contains?(name) if todo_items = find_index(name) return true else return false end end