Skip to main content

Showing Ruby, Rails and git info in your app

·206 words·1 min· ·
Ruby Rails Protip
Ariejan de Vroom
Author
Ariejan de Vroom
Jack of all Trades, Professional Software Craftsman
Table of Contents

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.

Related

Fast specs - Run your specs in less than 1 second
·1208 words·6 mins
Ruby Rails Rspec Testing Bdd Tdd Cucumber Fast_spec Fastspec Coreyhaines
RSpec speed-up (24.6%) by tweaking ruby garbage collection
·237 words·2 mins
Ruby Rails Rspec Speed Garbage Collection GC
Upgrading to Mongoid Beta 6
·197 words·1 min
Ruby Rails Mongoid Monogdb