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

Simon Goldman
Simon Goldman
1,028 Points

adding items to a list

not sure how to add it

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

grocery_item.values_at("items")

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I'm going to start by giving some hints here and hope that you can figure it out from there.

  • grocery_list contains an empty array at the key items
  • the push method can be used to add a grocery_item to this array
  • the grocery_item array can be referenced by using grocery_list['items']
  • push the grocery_item onto the items array

I hope this helps, but let me know if you're still stuck! :sparkles:

Simon Goldman
Simon Goldman
1,028 Points

grocery_item.push (grocery_list["items"])

still stuck not sure how to write it

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Oh close! So close but you've got it a bit backward. Take a look:

grocery_list['items'].push(grocery_item)

First, we say we're going to access the items array inside the grocery list. Then we push the grocery_item into the items list. Hope this helps! :sparkles: