Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
seal-mask
.a{fill-rule:evenodd;}techdegree
Nejc Vukovic
Full Stack JavaScript Techdegree Student 36,271 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Nejc Vukovic
Full Stack JavaScript Techdegree Student 36,271 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.