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 Working with Hashes That Contain Arrays

Help me with this code

the question is :- Let's add an item to our grocery list. We've set up a grocery_list hash that has an 'items' key with an empty array as its value. We've also created another hash and stored it in the grocery_item variable.

Append the grocery_item hash to the empty array that's under the grocery_list hash's 'items' key.

shopping_list.rb
grocery_list = { 'title' => 'Grocery List', 'items' => [] }
grocery_item = { 'title' => 'Bread', 'quantity' => 1 }
puts grocery_list["items"] = grocery_item
puts grocery_list

this code runs perfectly in workspaces fulfilling the the requirements of the question.

1 Answer

Steven Parker
Steven Parker
210,469 Points

You can certainly experiment with code in the workspaces, but you have to remember that there is nothing in the workspace that can confirm that the code is fulfilling the the requirements of the question.

In this case, the instructions say "Append the grocery_item hash to the empty array ...". But appending to an array isn't the same thing as replacing it with a different one, which is what this code is doing. Also, nothing in the instructions ask you to output anything, so you won't need "puts" here.

Hint: the "push" method of an array might be useful here.

okay , I shall do that ! thanks