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

Nejc Vukovic
Nejc Vukovic
Full Stack JavaScript Techdegree Student 36,258 Points

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?

Nejc Vukovic
Nejc Vukovic
Full Stack JavaScript Techdegree Student 36,258 Points

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.