74 lines
1.8 KiB
Markdown
74 lines
1.8 KiB
Markdown
|
---
|
||
|
title: "Showing Ruby, Rails and git info in your app"
|
||
|
kind: article
|
||
|
slug: showing-ruby-rails-and-git-info-in-your-app
|
||
|
created_at: 2012-04-02
|
||
|
tags:
|
||
|
- Ruby
|
||
|
- Rails
|
||
|
- protip
|
||
|
---
|
||
|
Some people've asked me how I show rendering information on [ariejan.net](http://ariejan.net).
|
||
|
|
||
|
![](https://ariejannet.s3.amazonaws.com/images/render_stats.jpg)
|
||
|
|
||
|
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
|
||
|
Rails.version
|
||
|
|
||
|
### Ruby version
|
||
|
|
||
|
Ruby also exposes version information, albeit using constants:
|
||
|
|
||
|
:::ruby
|
||
|
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
|
||
|
RUBY_PATCHLEVEL
|
||
|
=> 125
|
||
|
|
||
|
The you may also want to know which engine you're using. This may be "ruby" or something different:
|
||
|
|
||
|
:::ruby
|
||
|
RUBY_ENGINE
|
||
|
=> "ruby"
|
||
|
|
||
|
Combine these to get a sexy ruby version string:
|
||
|
|
||
|
:::ruby
|
||
|
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:
|
||
|
|
||
|
:::ruby
|
||
|
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:
|
||
|
|
||
|
:::ruby
|
||
|
# 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:
|
||
|
|
||
|
:::ruby
|
||
|
AppName::REVISION
|
||
|
=> "ac6d3a0"
|
||
|
|
||
|
You can now use this info through-out your app to show version information.
|