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

Joshua Goss
Joshua Goss
5,453 Points

"Try again!" That's all I get...

I've defined the methods need in order to push the hash value to the array correct?

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

list = create_list()
list['items'].push(list_item())

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 26,999 Points

That's a pretty cool solution. You look like you studied a functional-based programming paradigm.

The way the tests are written is simpler; it depends upon the mutable nature of the list inside the hash.

You simply want to add the grocery_item to the end of the items array contained in grocery_list

Try this piece of code. Good luck on your Ruby journey it's a cool language!

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

grocery_list['items'].push(grocery_item)
Joshua Goss
Joshua Goss
5,453 Points

Great! Thank you Jeff :)