Thursday, March 12, 2015

A little Ruby trick to DRY up your Gemfile

Here's a trick I learned today

The Problem

You've got a Ruby on Rails project with a Gemfile and a .ruby-version file at the root of your project, and, as is quite normal, your Gemfile contains the line ruby '2.1.0'. Now you want to update Ruby to 2.2.0 so you have to remember to change both the .ruby-version and the Gemfile which is a PITA and not especially DRY approach.

The Solution

A Gemfile is just Ruby, so instead of saying ruby '2.2.0' use ruby'.ruby-version').match(/(\d+\.\d+\.\d+)/).to_s instead. This will read the Ruby version info directly from your .ruby-version file and save you some hassle.

Neat huh.