Keeping track of Ruby gems in a Rails project

On a project I’m working on were are two developers. To keep track of the gems needed, we had a file in the root rails dir called GEMS.txt with a list of gems. Then I thought: verifying if I had the right gems installed is a repetitive task the computer should do for me. So I converted the text file to a YAML file, GEMS.yml, that looked something like this:

# Required gems
#
# Version example: version 0.7.5 or higher: ~>0.7.5
# Version example: exactly version 0.7.5: =0.7.5
-
  name: diff-lcs
-
  name: ZenTest
-
  name: RSpec
  version: =1.0.5

This means that you need any version of “diff-lcs” and “ZenTest” and exactly version 1.0.5 of RSpec. You would probably have more gems in there, but I’ve kept the list short for this blog post. Let’s say that another developer has added a new gem to GEMS.yml or changed the required version. Now you simply run “rake check_for_gems”:

$ rake check_for_gems
Gem found: diff-lcs 1.1.2
Gem found: ZenTest 3.4.2

2 of 3 required gems found.
Warning: Gem not found: RSpec ~>1.0.5

Either you don’t have the gem installed at all or you don’t have the correct version. So you install the correct version of the gem and now when you run “rake check_for_gems”, you get “3 of 3 required gems found.”

This is the source of the rake task: You can copy paste and save as lib/tasks/check_for_gems.rake

It’s just a simple tool, but it scratched an itch and was quickly made. Ruby is not only great for web applications on Rails, but also for small scripts like this.

Leave a Reply