Guest User

Untitled

a guest
Feb 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. require 'yaml'
  2.  
  3. def time_get
  4. puts "Please hit Enter to start time tracking"
  5.  
  6. if gets.chomp == ""
  7. Time.now
  8. else
  9. time_get
  10. end
  11. end
  12.  
  13. def time_stop
  14. puts "Please hit Enter again to stop time tracking"
  15.  
  16. if gets.chomp == ""
  17. Time.now
  18. else
  19. time_stop
  20. end
  21. end
  22.  
  23. def time_track
  24. start_time = time_get
  25. puts "Time tracking started at #{start_time}"
  26.  
  27. end_time = time_stop
  28. puts "Time tracking ended at #{end_time}"
  29.  
  30. end_time - start_time
  31. end
  32.  
  33. def time_format(time)
  34. Time.at(time).gmtime.strftime('%H:%M:%S')
  35. end
  36.  
  37. #gets all of the times stored in the times.yml file then totals them in total_times.yml
  38. def add_times
  39. totaled_times = YAML.load(open('times.yml'))
  40. total = totaled_times.inject(0) {|sum, time| sum = sum + time}
  41. open('totaled_times.yml', 'w') {|f| f << time_format(total)}
  42. end
  43.  
  44. #Storing all of the time entries in a yml file and reading them into an array
  45. all_times = YAML.load(open('times.yml'))
  46. all_times = [] unless all_times
  47. all_times << time_track
  48. open('times.yml', 'w') {|f| f << all_times.to_yaml}
  49.  
  50. add_times
Add Comment
Please, Sign In to add comment