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

help...im confused with this one to

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

http://teamtreehouse.com/library/ruby-collections/build-a-grocery-list-program/working-with-hashes-that-contain-arrays

6 Answers

Hi Lyric,

The challenge is asking you to append the dictionary grocery_item to the grocery_list items array, i.e the below.

grocery_list['items'].push(grocery_item)

Hi Chris

Thanks for your answer, it was really helpful to me! Are you able to explain to me why 'items' is within square brackets? Aren't square brackets indicative on an array? My understanding is that the array within the grocery_list variable is the value that pairs with the 'item' key, not the key itself.

Thanks in advance

Clair

Hi Clair,

The square brackets serve different purposes depending on the context in which you use them, for example in this code challenge we use the square brackets with a string value of items against the grocery_list variable as it's a dictionary and dictionaries offer the ability to select <key:value> pairs using the square brackets.

When used standalone however that infers an array which is what we're retrieving by targeting the items key in the dictionary.

Hope that helps.

Ok, that makes so much more sense now, thank you so much for taking the time to respond!

Thanks!

Thx guys! This is wrong:

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

This is ok:

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

When I did it in the command line, it shows:

{"title"=>"Grocery List", "items"=>[]}

and then when I put grocey_list on the command line, it shows the same.

Why is that? Thank you.

thanks

Nasser Sanou
Nasser Sanou
6,899 Points

grocery_list['items'].push(grocery_item)

I read the response, reread and didn't comprehend at all. :-( I know the formula for push is: hash1.push(hash2) but why grocery_list['items'].push(grocery_item) ??

Something wrong with me?