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 Collections Build a Grocery List Program Create a Method That Returns an Array

Checked my code and it displays an array but it still says error here on Treehouse

I don't know what's wrong. I checked this on the Workspace and it does display an array. Not sure why it says that it doesn't. What am I doing wrong?

array_method.rb
def create_list
  name = "Groceries"

  hash = { "name" => name, "items" => Array.new }
  return hash
end

def add_list_items
  item_name = "Bread"
  quantity = "1"

  hash = {"item" => item_name, "quantity" => quantity}
  return hash
end

list = create_list()
list['items'].push(add_list_items())

puts list.inspect
puts add_list_items().inspect

1 Answer

Hi Niki Torres .

If you are referring to the challenge then all you have to do is this:

def add_list_items
    return Array.new
end

And something I noticed but not relevant to the challenge at hand - but good to know:

quantity = "1"    //this could cause problems: when dealing with quantity you should use a number value and not a string as you do

quantity = 1    //this is better because you can only perform math operations with number values and not strings

Thanks! Noted about string vs numbers. But back to the challenge, I'm not getting why we have to do it that way. Am I not interpreting the instructions correctly?

Well you have to do it that way because the checking algorithm checks in the way that the challenge is set.

And you have to follow the instructions to the point.