Posted by Matt
on August 27, 2008
Here is a simple monkeypatch (duckpunch, donkeykick, squirrelbite, etc) to the Date class that easily allows us to calculate the number of weekdays between two dates. I can't believe that this functionality doesn't exist in the standard lib, but I'm normally the last person to mess with Date/Time calculations because they are absolute evil.
Drop this sucker into a file named "date.rb" in your Rails app's config/lib directory and restart.
Usage: Date.today.weekdays_until(some_date_in_the_future)
class Date
def weekdays_until(date)
return 0 if date <= self
(self..date).select{|day| day.is_weekday?}.size
end
def is_weekday?
self.wday != 0 && self.wday != 6
end
end
Posted by Matt
on August 25, 2008
I just translated an old PHP function to its Ruby counterpart. I think it really showcases Ruby's elegance:
PHP Version:
function calc_pct_complete($job_id, $d) {
$pc = 0;
$sql = "some SQL query";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res)) {
$x = total_days($row[start_date], $d) / total_days($row[start_date], $row[end_date]);
if ($x < 0) {
$x = 0;
} elseif ($x > 1) {
$x = 1;
} elseif ($row[name] != "X" && $row[name] != "Y") {
$x = $x * $x;
}
$pc += $x * $row[pct_hours];
}
mysql_free_result($res);
return $pc;
}
Ruby Version:
def percentage_complete(date = Date.today)
self.phases.inject(0.0) do |sum, phase|
percent_complete = phase.percentage_complete(date)
percent_complete = percent_complete ** 2 unless %w{X Y}.include?(phase.name)
sum += (percent_complete * phase.percentage_of_hours)
end
end
Posted by Matt
on August 24, 2008
While starting to enjoy Dan Ariely's book "Predictable Irrational", I come across a security tag firmly affixed to a page. I purchased this book directly from Amazon; so they have much trouble with shoplifters?
I understand protecting assets, but this ruined a paragraph of a book that I purchased.

Posted by Matt
on August 19, 2008
Everyone in the Rails community knows and loves Obie Fernandez (or they should). He gave a wonderfully insightful presentation at RubyFringe that everyone should take to heart.
Obie Fernandez - Do The Hustle
Posted by Matt
on August 19, 2008
Here are the slides from the lightening talk I gave last night at the Columbus Ruby Brigade meeting:
restful_acl.pdf