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


How to embed code snippets in blogger

Blogger.com (this blogging service) is terrible for embedding code.  Everything gets mangled, double-spaced and mis-formatted. Just terrible. But there is an answer to code snippet embedding: Gist.

Gist is a free service run by Github, the open-source project hosting service. Gist allows you to post code snippets share them via a unique URL, and even allows for comments. Just go to The Gist Website, paste in your code, give it a title and description, then click "embed" and copy the link. You can paste the link into your blog, and (using javascript) it will embed an un-mangled code snippet, just like this:


/*
* Load Prototype and script.aculo.us, then slide a div up and down.
*/
google.load("prototype", "1");
google.load("scriptaculous", "1");
var up = false;
function slideUpDown() {
if (up) {
Effect.SlideDown('content', {'duration' : 0.2});
up = false;
} else {
Effect.SlideUp('content', {'duration' : 0.2});
up = true;
}
}
function OnLoad(){
var content = $('content');
content.innerHTML = '<div>The Content</div>';
window.setInterval(slideUpDown, 600);
}
google.setOnLoadCallback(OnLoad);​
view raw Test code post hosted with ❤ by GitHub