Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Assignment 2 of 40 Days of Ruby
- # Author: SirBee
- # Date: 2013-12-31
- puts "\nAssignment 2"
- puts "SirBee forged this in a JRuby Enviroment (1.7.9)"
- puts "Runtime begins at: " + Time.new.to_s
- puts "At the task entry prompt you may enter any of these commands"
- puts "\n-s:\tSave List"
- puts "-e:\tEdit Item"
- puts "-d:\tDelete Item"
- puts "-q:\tQuit"
- tasks = []
- input = ""
- if !File.exist?('tasks.txt') # if tasks file not found, create one!
- puts "tasks.txt not found! Forging File..."
- File.new('tasks.txt', 'w') #create file
- end
- tasks = IO.readlines('tasks.txt') # readlines is a function in the IO class that feeds each new line in a file into a container
- loop do
- if tasks.size == 0 # if task container is empty, tell user
- puts "\nYou have no tasks assigned! Please enter Task!"
- else
- puts "\n"
- tasks.each_with_index { |task, index| puts (index+1).to_s + ": " + task.to_s } # print task index and task to console
- puts "\n"
- puts "Please Enter Task."
- end
- input = gets.chomp # enter task
- # check for option commands in input
- if input == "-q"
- break
- elsif input == "-d"
- puts "Which numbered task would you like to delete?"
- tasks.delete_at(gets.chomp.to_i - 1) # delete task at index
- puts "Task deleted"
- elsif input == "-e"
- puts "Which numbered task would you like to edit?"
- i = (gets.chomp.to_i - 1) # integer variable contains task index
- puts "Please Enter Task."
- tasks[i] = gets.chomp # overwrite that business!
- elsif input == "-s" # IO Save Option
- File.new('tasks.txt', 'w') # if tasks.txt is deleted during runtime, simply create new
- File.open('tasks.txt', 'w') do |file| # for each task in tasks, add new line
- tasks.each { |task| file.puts(task) }
- end
- puts "Tasks Saved!"
- else
- tasks << input # feed string into array
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment