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 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
228,978 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