Syntax Highlighting for Ruby Made Very Easy

29 May 2008

So I’ve been making do with lame-o ‘pre’ tags for my code snippets and my blog looks, well, lame. But the various alternatives for highlighting ruby either didn’t work for Blogger or smacked of effort (and you know how I feel about effort).

Well, recently I joined Obtiva and I was being all nosy looking at my new co-worker’s blogs, when I found Tyler Jennings post about creating Spotlight. Take some ruby code, paste it into the text box, press a button, and you get some html which you copy and past into your blog. You’ll have to include the default css in your css file the first time, but that’s not too hard. Output looks like this:

1 class Echo < ActionMailer::Base
2 def copy formatted_message, incident_id
3 users_with_echo_emails = User.find(:all,
4 :conditions => “echo_email is not NULL”)
5 users_with_echo_emails.reject! {|user| !user.receive_echo?}
6 recipients_array = users_with_echo_emails.map { |user| user.echo_email }
7 recipients_array « ENV[‘default_echo_email’] if ENV[‘default_echo_email’]
8
9 recipients recipients_array
10 subject “Globex pager incident #{incident_id} updated”
11 from ‘noreply@globex.com’
12 body :message => formatted_message, :incident_id => incident_id
13 end
14 end

Cool huh?