Showing Ruby, Rails and git info in your app

2 April 2012

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

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:

Rails.version

Ruby version

Ruby also exposes version information, albeit using constants:

RUBY_VERSION
=> "1.9.3"

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

RUBY_PATCHLEVEL
=> 125

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

RUBY_ENGINE
=> "ruby"

Combine these to get a sexy ruby version string:

puts "#{RUBY_ENGINE}-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
=> "ruby-1.9.3-p125"

Current process ID

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

Process.pid
=> 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`
end

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

AppName::REVISION
=> "ac6d3a0"

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