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
171,342 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