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

Grocery list loop

Im having trouble writing a code that lets my program run in a loop to add more items to the list until I say "N". I've seen codes for this extra credit but honestly don't understand them and why they work. I'm hoping that someone can help me understand how to reference a method within another method and have it loop smoothly.

below is my code.

def create_list print "what is the name of the list? " name = gets.chomp.downcase hash = {"listname" => name, "items" => Array.new} return hash end def add_list_item print "what is the name of the item? " item_name = gets.chomp

print "how many? " quantity = gets.chomp.to_i

hash = {"name" => item_name, "quantity" => quantity} return hash add_new_item end def add_new_item

print "would you like to add another item? Enter: y or n: " add_new_item = gets.chomp.downcase if add_new_item == "y" return add_list_item else puts "you have created a list homie" end end

list = create_list() list["items"].push(add_list_item()) add_new_item

1 Answer