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

need help please!

Do i use merge or what?

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

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You need to put the whole grocery_item hash inside the 'item' array, which in turn constitutes a value inside another hash. So first you want to access that array as the key:

grocery_list['items']

Next, you want to add the hash to that key. How do we insert objects into arrays? One way, used by Jason in the video,would be:

grocery_list['items'].push(grocery_item)

This will also work:

grocery_list['items'] << grocery_item