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

Need help with ruby

Hi everyone, I am not understanding the code down below, could any of you guys please help me just break down each line of code so that I can interpret it. For example, how does puts "List: #{list['name']}" even access the name. If you don't know where the code is from, it is from grocery list ruby part 3, thanks all

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

1 Answer

Hello,

I will try my best to break it down for you.

def print_list(list)

end

I'll start with the opening and closing of print_list. Between the () you have the list you will pass to it. This list is formed in an earlier lesson, so you get access to it that way, by assigning that to a variable earlier. Anything we pass to this whether we've named it mylist or anylistnameever. It will be reassigned to the name list. As this allows it to remain consistent and not have to rewrite this method every time to accommodate different unique names.

puts "List: #{list['name']}"

This is a particularly important line and is similar in later lines of this code. You gain access to the name of it by the list variable, that got passed earlier. 'name' is a key, and when you use square brackets on an array and pass a key, it will give you the value of that key. A key is a consistent way to identify something.

puts "----"

Just output some hyphens for separation.

list["items"].each do |item|

end

So this is probably the biggest part to understand. We're doing similar to the 'name' key earlier, except we're now looping on that. This "items" key will have multiple values, and to access them when we don't know what they will be called we must loop through them. This is done by assigning them to the item variable, the | alongside it are just so ruby knows, with each item between here assign it to the item variable.

puts "Item: " + item['name']
puts "Quantity: " + item['quantity'].to_s

So we output this data in the name way we have for the 'name' earlier. Except it refers to a different 'name' key. The same applies to 'quantity' except this is being made into a string with the .to_s method. This is mentioned in the lesson why it's necessary, so I shouldn't need to repeat that.

puts "----"

Just output some hyphens for separation.

Hopefully this helps.