Ruby Backquotes
Wednesday, November 22nd, 2006I learnt a useful but very simple thing with Ruby today. Using backquotes, you can return the standard output of running cmd in a subshell.
e.g.
> `date`
> "Wed Nov 22 19:11:32 GMT 2006"
Now think about how you could use this in Rails to display the subversion revision of your web application. Simple, using YAML and the svn info command.
The svn info command returns the following output.
Path: .
URL: http://myrepository.tld/svn/projects
Repository Root: http://myrepository.tld/svn/projects
Repository UUID: bab91cb8-3837-db11-9351-0014220fd224
Revision: 143
Node Kind: directory Schedule: normal
Last Changed Author: goatley
Last Changed Rev: 143
Last Changed Date: 2006-11-22 17:57:18 +0000 (Wed, 22 Nov 2006)
This looks like YAML to me, so we can load the output into the YAML object.
> REVISION = YAML.load(`svn info`)['Revision']
> 143
Very useful!