Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def create_list (items)
items_arr = items.split(" ")
grocery_list = {}
items_arr.each do |items|
grocery_list[items] = 1
end
return grocery_list
end
def add_item(grocery_list, item, quantity)
grocery_list[item] = quantity.to_i
return grocery_list
end
def remove_item(grocery_list, item)
grocery_list.delete(item)
return grocery_list
end
def update_item (grocery_list, item, quantity)
if grocery_list[item] == nil
puts "Item does not exist; please add item if desired"
else grocery_list[item] = quantity.to_i
end
grocery_list
end
def print_grocery_list(grocery_list)
grocery_list.each do |item, quantity|
puts "You have entered #{item}; you need #{quantity} of them"
end
end
list = create_list("carrots apples cereal pizza")
puts list
list = add_item(list, "celery", 1)
puts list
list = remove_item(list, "carrots")
puts list
list = update_item(list, "celery", 3)
puts list
list = update_item(list, "grapes", 4)
puts list
print_grocery_list(list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment