Skip to content

Instantly share code, notes, and snippets.

@renaehodgkins
Created December 1, 2008 03:27
Show Gist options
  • Save renaehodgkins/30605 to your computer and use it in GitHub Desktop.
Save renaehodgkins/30605 to your computer and use it in GitHub Desktop.
require 'yaml'
class TimeTrack
def initialize
@time_list = 'times.yml'
#Storing all of the time entries in a yml file and reading them into an array
prepare_file(@time_list)
all_times = YAML.load(open(@time_list))
all_times = [] unless all_times
all_times << time_track
open(@time_list, 'w') {|f| f << all_times.to_yaml}
add_times
end
def time_get
puts "Please hit Enter to start time tracking"
if gets.chomp == ""
Time.now
else
time_get
end
end
def time_stop
puts "Please hit Enter again to stop time tracking"
if gets.chomp == ""
Time.now
else
time_stop
end
end
def time_track
@start_time = time_get
puts "Time tracking started at #{@start_time}"
@end_time = time_stop
puts "Time tracking ended at #{@end_time}"
@end_time - @start_time
end
def time_format(time)
Time.at(time).gmtime.strftime('%H:%M:%S')
end
#gets all of the times stored in the times.yml file then totals them in total_times.yml
def add_times
totaled_times = YAML.load(open(@time_list))
total = totaled_times.inject(0) {|sum, time| sum = sum + time}
prepare_file('totaled_times.yml')
open('totaled_times.yml', 'w') {|f| f << time_format(total)}
end
private
def prepare_file(filename)
if !File.exist?(filename)
File.new(filename, 'w')
end
end
end
track = TimeTrack.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment