Friday, June 26, 2009

Installing Ruby 1.9 on Mac OS X 10.5

Tommie over at The blog with no name has written up a how-to on getting Ruby 1.9 up and running in Mac OS X 10.5.7. Check it out: http://www.tommycampbell.net/2009/06/26/ruby-1-9-on-mac-os-x

Labels:

Thursday, June 4, 2009

How to optionally load gems

I ran into an interesting problem today at work. I wanted to optionally require a ruby gem. For example, I wanted to include the metric_fu rake tasks, but if a user doesn't have metric_fu installed, it's no big deal. Here's how I implemented it. I added a method to the Kernel space (where require lives) called desire. I desire this gem to be installed, but I should just see a warning if it's not installed, instead of an error. Here's the desire method (I put this in RAILS_ROOT/Rakefile).
module Kernel
  def desire(library)
    require library
    return true
  rescue LoadError
    STDERR.puts "warning: #{library} gem desired but not found."
    return false
  end
end
And here's how I optionally include the metric_fu gem.
desire 'metric_fu'
If a user has the gem installed, rake -T will show the tasks.
jasonn:~/sources/my_files [master] $ rake -T
(in ~/sources/my_files)
rake metrics:all 
[...]
jasonn:~/sources/my_files [master] $
If the user does not have the gem installed, rake -T will show a simple warning and continue.
jasonn:~/sources/my_files [master] $ rake -T
(in ~/sources/my_files)
warning: metric_fu gem desired but not found.
[...]
jasonn:~/sources/my_files [master] $
I don't remember where I first heard the desire idea, but it works pretty well.

Wednesday, June 3, 2009

Recover files from git after deletion is committed

As I work with git, I have occasionally deleted files that I later end up needing. You can restore the files, but it's not an intuitive process. First, I looked at the output of git log to find the commit that I deleted the files with. Then I ran:
git diff --diff-filter=D --name-only HEAD@{'7 days ago'} | \
grep -v ^doc | perl -ne 'chomp; 
print "git show e2ae03f14ce81a3e24ec6cbe2e73767f4c6fed1b -- $_ > $_\n";' | sh
This restored all the files I deleted that did not start with doc. These files show up as diff files, so you may have to remove the "- " at the beginning of the lines and commit messages. But hey, it's better than not having the files you need.