Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created April 19, 2022 20:12
Show Gist options
  • Save caioertai/01a410403bceca4f5ef4d7f028522c5e to your computer and use it in GitHub Desktop.
Save caioertai/01a410403bceca4f5ef4d7f028522c5e to your computer and use it in GitHub Desktop.
# Ternary operator syntax
# condition ? truthy_statement : falsy_statement
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
# Welcome (puts some welcome string to the user)
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
# Ask user for the index of the gift to be marked
# Get the gift of the given index --- gift => { name: "Teddy Bear", bought: false }
# Update (remember CRUD) the :bought value to true
when "quit"
puts "Goodbye"
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment