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 trialMikey Neilson
Front End Web Development Techdegree Student 12,642 Pointsremoving item from shopping list with ruby
hey i'm after a little help with making a basic shopping list with ruby, I want user to be able to add items and remove if they wish.I have got the add items part working but i'm struggling with the remove items . Any help with this would be amazing here is my code so far.......
def create_list
print "whats the name of the list: "
name = gets.chomp.capitalize
hash = {"name" => name, "items" => Array.new}
return hash
end
def add_to_list
print "What items do you want to add: "
item_name = gets.chomp.capitalize
print "How much: "
quantity = gets.chomp.to_i
hash = {"name" => item_name, "quantity" => quantity}
return hash
end
def print_list(list)
puts "-" * 30
puts "Name: #{list['name']}"
puts "-" * 30
list['items'].each do|item|
puts "Name: #{item['name']}"
puts "Quantity: #{item['quantity']}"
end
puts "-" * 30
end
def remove_item(list)
print "Do you want to remove item from shopping list? Enter y/n: "
answer = gets.chomp
if answer == "n"
print "Ok your #{list['item_name']} is finished"
elsif answer == "y"
print "Enter the item you would like to remove: "
remove = gets.chomp.capitalize
list['items'].each do|i|
end
return i
end
#list['items'].delete(remove)
else
print "answer y/n only. "
end
return remove
end
list = create_list()
list['items'].push(add_to_list())
list['items'].push(add_to_list())
print_list(list)
remove = remove_item(list)
remove['list'].delete(remove_item(remove))
print_list(list)