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

Sam Fitz
Sam Fitz
7,036 Points

How to insert a Hash into an Array, which is itself also inside a Hash? (in Ruby)

I'm afraid I'm quite lost on how to do this. There was some usage of the .each command in the video leading up to this challenge, but even that I have a tentative grasp on, and this seems to be territory that wasn't even directly touched in the video.

Any help would be appreciated. I've done several Google searches, but haven't been able to find any instances where someone is asking about how to insert a Hash into an Array which is in a Hash....

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

items = [{grocery_item}]

2 Answers

Sam Fitz
Sam Fitz
7,036 Points

Maciej, you came through for me again! Thank you!

And I'm glad the solution is simple and elegant compared to what I presumed.