Guest User

Untitled

a guest
May 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. class Todo
  2. def initialize(*argv)
  3. @argv = argv
  4.  
  5. begin
  6. send(@argv[0])
  7. rescue TypeError
  8. list
  9. rescue NoMethodError
  10. puts "Command #{@argv[0]} not found.\n\n"
  11. help
  12. end
  13. end
  14.  
  15. # Lists help information
  16. def help
  17. puts <<-help
  18. Commands for Todo.rb:
  19. add [task name] - Add a new task
  20. list - Lists all tasks
  21. done [task id] - Complete a task
  22. help - Prints out this information
  23. help
  24. end
  25.  
  26. # Add task
  27. def add
  28. unless @argv[1]
  29. puts "Lacking argument [name]"
  30. exit
  31. end
  32.  
  33. # Append task to file
  34. contents = File.read('todo.td')
  35.  
  36. File.open('todo.td', 'w') do |f|
  37. todo = contents + @argv[1] + "\n"
  38. f.write(todo)
  39. end
  40. end
  41.  
  42. # List all tasks
  43. def list
  44. # Read content
  45. contents = File.read('todo.td')
  46. puts "No tasks" unless contents
  47.  
  48. # Show it with ids
  49. i = 0
  50. contents.each_line do |todo|
  51. i += 1
  52. puts "##{i} - #{todo}"
  53. end
  54. end
  55.  
  56. # Finished a task
  57. def done
  58. unless @argv[1]
  59. puts "Lacking argument [id]"
  60. exit
  61. end
  62.  
  63. # Put tasks into an array
  64. todos = File.read('todo.td').split("\n")
  65.  
  66. unless todos
  67. puts "No tasks"
  68. exit
  69. end
  70.  
  71. puts "Completed task: " + todos[@argv[1].to_i - 1]
  72.  
  73. # Delete task from array and make string
  74. todos.delete_at(@argv[1].to_i - 1)
  75. content = todos.join("\n")
  76.  
  77. # Update file
  78. File.open('todo.td', 'w') do |f|
  79. f.write(content)
  80. end
  81. end
  82. end
  83.  
  84. todo = Todo.new(*ARGV)
Add Comment
Please, Sign In to add comment