Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: