Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created April 19, 2022 21:39
Show Gist options
  • Save caioertai/975c77efccf94ebbf6e472de413ae2d4 to your computer and use it in GitHub Desktop.
Save caioertai/975c77efccf94ebbf6e472de413ae2d4 to your computer and use it in GitHub Desktop.
require_relative "xmas_list"
puts "Welcome to your Christmas gift list"
gifts = [
{ name: "Necklace", bought: true }, # 0
{ name: "Teddy Bear", bought: false }, # 1
{ name: "Ring", bought: false } # 2
]
loop do
puts "What do you want to do? (list / add / delete / mark)"
action = gets.chomp
case action
when "list"
display_gifts(gifts)
when "add"
puts "What's the name of the gift you want to add?"
gift_name = gets.chomp
new_gift = { name: gift_name, bought: false }
gifts << new_gift
when "delete"
display_gifts(gifts)
puts "What do you want do delete? (pick by number)"
gift_index = gets.chomp.to_i - 1
gifts.delete_at(gift_index)
when "mark"
# Display all gifts
display_gifts(gifts)
# Ask user for the index of the gift to be marked
puts "Which gift you want to mark as bought?"
gift_index = gets.chomp.to_i - 1 # 2 will mean index 1, and so on...
# Get the gift of the given index --- gift => { name: "Teddy Bear", bought: false }
bought_gift = gifts[gift_index] # => a gift (a Hash)
# Update (remember CRUD) the :bought value to true
# { name: "Teddy Bear", bought: false }
bought_gift[:bought] = true
# { name: "Teddy Bear", bought: true }
when "quit"
puts "Goodbye"
break
end
end
def display_gifts(gifts_array)
gifts_array.each_with_index do |gift, index|
checkbox = gift[:bought] ? "[x]" : "[ ]"
puts "#{index + 1}. #{checkbox} #{gift[:name]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment