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

I am really not getting the ruby grocery list and am getting extremely frustrated for the last 2 days

Hi every one, I really need your help breaking down the code from the grocery list part 3 in Ruby


I need help with this part as I don't get how anything below inside the dashes works and I have been trying for 2 days. ruby is my first language, so it would be appreciated if you can explain it to me. What really throws me of is how the code works below If you can give an in-depth explanation, it would help sooo much. Is it also normal to struggle like this learning your first language

def print_list(list) puts "List: #{list['name']}" puts "----"

list["items"].each do |item| puts "Item: " + item['name'] puts "Quantity: " + item['quantity'].to_s puts "---" end end

list['items'].push(add_list_item())


2 Answers

Hi Aaron, so it's a method to output the list. The list is a hash consisting of a list name, and then an array of items within the hash. What we're doing here is on the first line, outputting the value of the list name, followed by a spacer of sorts (---).

After that, what's happening is that we're "iterating" through the items array part of the hash. We output some formatting text (Item: ) and then the item name.

Next, outputting more formatting text and then the item quantity (converted to string) and a spacer.

Then, ending the .each loop, and ending the method.

The last line is a different method... shouldn't be mixed in with this. It is for adding items to the list.

Thanks shaungzhou for your quick reply, my only last question is in this code for example "Item: " + item['name'] puts how does the program know to look through item and find the name cause I thought the array was called items with the plural s

Also, what does list['items'].push(add_list_item()) do thanks

Garrett Carver
Garrett Carver
14,681 Points

edit: fixed formatting, and missing brackets

Responding to this question:

"Also, what does list['items'].push(add_list_item()) do thanks"

So to break it down:

list is a hash, which contains two keys (everything will go under this hash). It gets created when we call list = create_list()

items is a key in our 'list' hash. When we called the method create_list() it first created the list hash, then it put an empty array as the value for the key items, so we just want to push stuff into that empty array.

add_list_item() returns only one grocery item in the form of a hash. It has the keys name and quantity.

push is the ruby method which adds items to an array. In this case, each time we push, it adds an item to the empty array inside the original hash. But since we are keeping track of not only the names of the grocery items, but the quantity as well, we are actually putting a hash for each index in the array. So our list hash looks something like:

{ "name" => name, "items" => [{"milk" => 1}, {"eggs" => 2}] }

So to simplify it a bit, let's say we don't care about the quantities on our list of groceries. If I wanted to just put strings of the items we want instead of hashes in the array under the items key, I would write:

list['items'].push("milk")

list['items'].push("eggs")

Then our hash list would be like this:

{ "name" => name, "items" => ["milk", "eggs"] }