Skip to content

Instantly share code, notes, and snippets.

@forresty
Created January 24, 2018 01:29
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 forresty/56d8764bbbc1b12ea849fcec2bc750ff to your computer and use it in GitHub Desktop.
Save forresty/56d8764bbbc1b12ea849fcec2bc750ff to your computer and use it in GitHub Desktop.
# 0. init the todo
# items = %w{ tomato potato eggs socks }
items = {
'tomato' => false,
'potato' => true,
'eggs' => false
}
def prompt(msg)
puts msg
print '> '
return gets.chomp
end
def handle_add(items)
item = prompt('enter item to add:')
items[item] = false
end
def display_items(items)
p items
item_with_ids = {}
index = 1
items.map do |item, done|
mark = done ? 'X' : ' '
puts "#{index}. [#{mark}] #{item}"
item_with_ids[index] = item
index += 1
end
return item_with_ids
end
def handle_remove(items)
item_with_ids = display_items(items)
p item_with_ids
item_id = prompt('enter item id to remove:').to_i
item = item_with_ids[item_id]
items.delete(item)
end
def handle_mark(items)
item_with_ids = display_items(items)
item_id = prompt('enter item id to mark as done:').to_i
item = item_with_ids[item_id]
items[item] = true
end
# 1. print welcome
puts "-" * 30
puts "welcome to xmas gift"
puts "-" * 30
loop do
input = prompt('choose your action[list/add/remove/mark/quit]')
case input
when 'list'
display_items(items)
when 'add'
handle_add(items)
when 'remove'
handle_remove(items)
when 'mark'
handle_mark(items)
when 'quit'
exit
else
puts "unknown command, try again!"
end
end
# if input == 'list'
# elsif input == 'add'
# elsif input == 'remove'
# end
# 4. goto 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment