I have a program that lists all of my company’s projects and various statistics about them. Between little APIs, background services, and large applications, we’re getting quite a collection of Rails projects. I decided to add a column to show which version of Rails each was running—a column that would be automatically updated.

It turns out, it’s pretty easy to do with grit and bundler.

require "grit"
require "bundler"

repo = Grit::Repo.new(PROJECT_PATH)
lockfile = repo.tree/"Gemfile.lock"
locked_gems = Bundler::LockfileParser.new(lockfile.data)
rails = locked_gems.specs.find { |spec| spec.name == "rails" }
rails.version