Showing Ruby, Rails and git info in your app

Some people’ve asked me how I show rendering information on

There are a few things going on here, let me explain them one by one.

Rails version

The current Rails version is probably the easiest you see here. Rails exposes its version information like this:


Ruby version

Ruby also exposes version information, albeit using constants:

=> "1.9.3"

You may know that ruby also has different patch levels for each release. You can also retrieve that information:

=> 125

The you may also want to know which engine you’re using. This may be “ruby” or something different:

=> "ruby"

Combine these to get a sexy ruby version string:

=> "ruby-1.9.3-p125"

Current process ID

I like to know which Unicorn produced a certain page. Retrieving the process ID is easy:
=> 9473

Current git revision SHA

Knowing which version of your app is currently running can be useful information. To do this I created the following initializer:

# config/initializers/git_revision.rb
module AppName
  REVISION = `git log --pretty=format:'%h' -n 1`

This will expose the current short SHA in your application’s namespace:

=> "ac6d3a0"

You can now use this info through-out your app to show version information.