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

Issues adding a hash into an array

I keep getting stuck trying to add the grocery_item hash into the items array of the grocery_list hash. I tried to separate out the values from the grocery_item hash to add those to the items array but all Ive done is confuse myself more. My code is attached, any insight would be helpful. Thanks.

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

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

They want the hash to be one of the elements of the array. The array is now empty and it needs to include one element - a hash. It's supposed to look like this:

'items' => [{ 'title' => 'Bread', 'quantity' => 1 }]

You code seems to be doing this:

'items' => { 'title' => 'Bread', 'quantity' => 1 }

One of the simplest solutions looks like this:

grocery_list['items'] << grocery_item

"Insert the grocery_item hash into the array which is under 'items' key in grocery_list hash"

Thanks Maciej.