Created
January 23, 2018 17:16
-
-
Save Spone/1d6e81f5fdd7f0d4def61ba420dba8c5 to your computer and use it in GitHub Desktop.
Revisions
-
Spone created this gist
Jan 23, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ require 'json' puts "Welcome to the Chrismas list!" # Settings filepath = "list.json" # Load the items from the JSON file (only if it exists) if File.exist?(filepath) serialized_list = File.read(filepath) items = JSON.parse(serialized_list, symbolize_names: true) else items = [] end action = nil until action == "exit" puts "What do you want to do? [add, list, delete, mark, exit]" # User can type the action action = gets.chomp # Perform the action case action when "add" puts "What do you want to add to the list?" item = gets.chomp items << {name: item, marked: false} puts "#{item} has been added to the list" when "list" if items.empty? puts "There is nothing in your list." else items.each_with_index do |item, index| puts "[#{item[:marked] ? "x" : " "}] #{index + 1}: #{item[:name]}" end end when "delete" items.each_with_index do |item, index| puts "[ ] #{index + 1}: #{item[:name]}" end puts "Which item do you want to delete?" index_to_delete = gets.chomp.to_i - 1 puts "Are you sure you want to delete #{items[index_to_delete][:name]}? y/n" confirmation = gets.chomp if confirmation == "y" items.delete_at(index_to_delete) else puts "Okay, #{items[index_to_delete][:name]} is not deleted." end when "mark" items.each_with_index do |item, index| puts "[#{item[:marked] ? "x" : " "}] #{index + 1}: #{item[:name]}" end puts "Which item do you want to mark?" index_to_mark = gets.chomp.to_i - 1 items[index_to_mark][:marked] = true # If the user typed the name, we need to do: # items.each do |item| # if item[:name] == name_from_the_user # item[:marked] = true # puts "Okay, item was marked" # end # end when "exit" puts "Goodbye! Merry Christmas!" end end # Save all items in a JSON file File.open(filepath, 'wb') do |file| file.write(JSON.generate(items)) end