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

kevinthecoder
kevinthecoder
10,791 Points

Unable to add Grocery Hash to Grocery_List Array (3rd item inside)

Alright, this one is really hard. I have tried this about 20 different ways over half an hour straight; also re-educated myself with the videos three times now and carefully watched the syntax, parenthesis, curly braces etc. This one is super tricky to me. I even tried defining a new method and returning the grocery hash inside the method and then pushed those two together while being aware that it has to go into the third argument of grocery_list (example: 2) as it's an array. I'm totally stumped. Any help would be appreciated. The good news is that I got through the first 3 of 4 challenges on the first try . What am I doing wrong??

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

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Add the grocery_item hash to the items array inside of the grocery_list hash.

what you should do here is add the whole grocery_item hash into the grocery_list['items'] empty Array.

grocery_list['items'].push(grocery_item)
kevinthecoder
kevinthecoder
10,791 Points

ARGH! So close! The funny thing is that I even previously tried the [brackets] several times as I knew it was an array. However, I didn't pair it correctly with the last part exactly as you have written above. Then there's the flip side to that too....at one point, I tried only the single parentheses for grocery_item but didn't get the array portion exactly right. So frustrating (sigh)....

Thanks for your help.