Sunday, March 6, 2011

When life hands you lemons...

If you're unemployed, and want to know how many days it's been, how many years experience you have, and with which companies, this Ruby class will tell you. Basically, initialize the WorkHistoryAnalysis class, pass in each experience as "<name>, [start date], [end date]" in YYYY,MM,DD format as shown at the bottom of the file. A little report will be generated at the console.

require 'date';
class WorkHistoryAnalysis
attr_accessor :total, :company, :latest
def initialize()
@total_days = 0
@company = Hash.new
@latest = Date.new
end
def workedFor(who, from, to)
to_d = Date.new(to[0],to[1],to[2])
from_d = Date.new(from[0],from[1],from[2])
latest?(to_d)
days = (to_d - from_d).to_i
if (@company.has_key?(who))
@company[who] += days
else
@company[who] = days
end
@total_days += days
end
def latest?(dat)
if ((dat <=> @latest) > 0)
@latest = dat
else
return false
end
return true
end
def employed
years = days2yrs(@total_days)
return ["Employed for:"],[" #{years} years [#{@total_days} total days]"]
end
def unemployed
days = (Date.today - @latest).to_i
years = days2yrs(days)
return ["Currently unemployed for:"],[" #{years} years [#{days} days]"],["Last worked:"],[" #{@latest.to_s}"]
end
def days2yrs(days)
years = days/365.to_f
return (years * 100).round.to_f / 100
end
def list_experiences
str = "Employment breakdown by company:\n"
co = @company.sort_by { |k,v| v }
co.each { |a| str += " #{a[0]} #{days2yrs(a[1])} years [#{a[1]} days]\n" }
return str
end
def print
puts employed
puts unemployed
puts list_experiences
end
end
W = WorkHistoryAnalysis.new
W.workedFor("NWlink",[1996,2,1],[1997,7,1])
W.workedFor("Orrtax",[1997,8,1],[2000,1,1])
W.workedFor("Hostpro",[2000,2,1],[2001,11,1])
W.workedFor("College",[2002,4,1],[2005,12,16])
W.workedFor("Microsoft",[2006,1,1],[2006,5,1])
W.workedFor("Marchex",[2006,6,1],[2008,4,1])
W.workedFor("Microsoft",[2008,10,1],[2009,9,26])
W.print
view raw ExperienceAdder hosted with ❤ by GitHub


No comments:

Post a Comment