Skip to content

Instantly share code, notes, and snippets.

@common-nighthawk
Created April 8, 2016 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save common-nighthawk/3f80a2a391ecd331e6037fde46c94716 to your computer and use it in GitHub Desktop.
Save common-nighthawk/3f80a2a391ecd331e6037fde46c94716 to your computer and use it in GitHub Desktop.
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