Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
namespace :gems do namespace :downloads do desc 'Print list of Gem downloads from rubyforge.org' task :list do require "rubygems" require "open-uri" require "hpricot" def print_gems content = "" open("http://gems.rubyforge.org/stats.html").each do |line| content << line end doc = Hpricot(content) rows = [] doc.search("table/tr").each do |row| rows << row.search("/").collect {|e| e.inner_html} end max_lengths = get_max_lengths(rows) print_dashes max_lengths print_row rows[0], max_lengths print_dashes max_lengths rows[1..-1].each do |row| print_row(row, max_lengths) end print_dashes max_lengths end def get_max_lengths(rows) max_lengths = [0, 0] rows.inject(max_lengths) do |max_lengths, row| max_lengths[0] = row[0].length if row[0].length > max_lengths[0] max_lengths[1] = row[1].length if row[1].length > max_lengths[1] max_lengths end max_lengths end def print_dashes(max_lengths) puts '-' * (max_lengths[0] + max_lengths[1] + 7) end def print_row(row, max_lengths) printf("| %#{max_lengths[0]}s | %#{max_lengths[1]}s |", row[0], row[1]) puts end print_gems end end end
This paste will be private.
From the Design Piracy series on my blog: