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 Returning Boolean Values: Part 2

kevinthecoder
kevinthecoder
10,791 Points

Unable to complete contains method.

Alright, this one has me stumped too. To 'contain' something really means to 'include' it, hence we use the include? method as indicated in my Dave Thomas book Programming Ruby. I have tried it with and without the (name) argument and with and without the if statement. My initial thoughts were that one did not need the if statement nor did you need to include the (argument). I am pretty confident on the 'return true' line. I know I'm close but this escapes me, despite reading in my book plus the videos. Could use some help please. I guess Booleans is now my weak point/spot when it comes to Ruby (so far).

todo_list.rb
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)
    if todo_items.include?(name)
      return true
  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

Topic: include? vs find_index

Hi,

I'm new to Ruby. After several frustrating hours, I came to this forum. i used the find_index method that I found on another question/answer post in this forum. Is there a preferred answer between the methods include? and find_index to solve the contains? challenge?

Thank you

3 Answers

Hi Kevin,

Yes, correct, we should use include? method, however it wouldn't work to pass it in the challenge as I tried.

def contains?(name)
  @todo_items.include?(name)
end

But we can use different method like .each, I explained similarly to previous thread. :-)

kevinthecoder
kevinthecoder
10,791 Points

OK. How interesting; that one really threw me off as well. I was going to try the forum initially but I noticed that there weren't any forum questions listed right away under that tab (that's where I was looking before on other topics/lessons). Next time, I'll instead just manually go to the forum (on the left side of screen) and search for what I need to see if others have already commented on a particular item (like you did). Thanks!

This Boolean stuff is probably the toughest stuff I've encountered so far. Before this, the toughest items to understand was the hash/array stuff but even now, the array stuff (i.e. {#name} , the exact characters and putting everything in correct syntax and order are starting to make sense to me. Practice makes perfect, I suppose. If one pounds something over and over again, eventually, it clicks, right?

Like the array/hash stuff, perhaps the Boolean stuff will be easier to learn over time.

Yup, remember how tough to learn hash/array when you look at this method for the first time before you get concepts for a while, now this Boolean topic is a bit more challenging. I can suggest you to use different tutorial resources to understand Boolean concepts if we didn't get it from this Ruby tutorial.

Keep trying and moving on. :-)