Cleaning up a messy Gemfile

Aging Rails apps can become a bit of a mess. Fortunately we can fix this bit by bit. Today we will look at some rules for writing a clean, readable Gemfile.

1. The gems should be split into groups. (Test, Development, etc.)

Any Gemfiles that look like this can get messy as they get longer:

gem 'nokogiri'
gem 'sinatra'

gem 'wirble', group: :development
gem 'faker', group: :test
gem 'rspec', group: :test

gem 'capybara', group: :development
gem 'rspec-rails', group: :development

This can be as many groups as you like but they should be in this format:

# Gems used everywhere.
gem 'nokogiri'
gem 'sinatra'

group :development do
  gem 'wirble'

group :test do
  gem 'faker'
  gem 'rspec'

group :test, :development do
  gem 'capybara'
  gem 'rspec-rails'

2. Within those groups the gems should be alphabetically ordered.

This just makes sense so that they can be found easily. You wouldn’t want to walk into an unordered library, would you?

3. Remove any gems that have bee commented out.

You don’t need to keep the entire history of the Gemfile. That is what git is for.

4. No comments on it’s own line if the comment refers to a gem.

Comments in the Gemfile usually aren’t needed. You can learn what each of the Gems do. So instead of this,

# Thin. Web Server greatness
# Start with: rails s
gem 'thin'

let the comment sit on the line with the gem:

gem 'thin' # Start with rails s

5. Only use apostrophes or quotations marks, not both. Pick one and change the other.

You can pick between gem ‘rails’ or gem “rails” but the style should be consistent.

This isn’t a conclusive list but it should help cut down the size and complexity of your Gemfile.

Success! You're on the list.

Ruby off Rails

So, Ruby is well know for its hugely popular gem Rails which greatly simplifies building websites. But what are some other programs built in Ruby? Here are a few very popular examples.

RubyMotion – Develop native apps for iOS (both iPhone and iPad) and OS X in Ruby. It is based on MacRuby which was developed by Apple. The app compiles into native objective-c just like iPhone apps made traditionally.

Artoo – Gets Ruby up and running on Arduino, Leap Motion, Pebble, Rasberry Pi, Ardrone, Roomba and more. Built to let you program devices and the internet of things in Ruby. It also lets you connect multiple devices together through Artoo into an integrated robotic solution.

Rake – A task management tool that allows you to namespace tasks and run them with Ruby it is Ruby’s alternative to make. A simple gem that you can install with ‘gem install rake’. Built by Jim Weirich (who you need to know about), it enables you to run a command from the bash prompt and execute Ruby code inside your app or on the system at large. Wikipedia claims that “It is the most widely downloaded Ruby Gem, downloaded more than 40 million times[12] and has been included with Apple OS X since at least version 10.7.”

Chef – A system provisioning tool that speeds up the initial configuration of servers as well as their maintenance. It hooks into cloud services like AWS and Rackspace. It works on the basis of “recipes” that define how a given task is to be done for example how packages are to be installed and what programs should be running on a server.

Capistrano – It started as mainly a rails deployment tool, but can be used to deploy any language webapps now. It sells itself as a remote server automation tool that can deploy to an arbitrary number of web servers and to automate common development tasks.

Homebrew – A command line package manager for OSX that is a must have for any Ruby developer. Easily install databases like mysql and postgres or download python with ‘brew install python’ to try it out. It is powered by git and ruby so you can hack at it as you like. You can also create your own homebrew packages with ‘brew create’ and share them with the world.

Sinatra – “Put this in your pipe and smoke it.” Sinatra is a lightweight alternative to Rails. Use rvm to ‘gem install sinatra’ and then build out a small webapp.

Gosu – A 2d game creation gem that works on Mac, Windows and Linux.

Ocra – Creates Windows applications from Ruby code. Includes the Ruby code and interpreter in the application.


Success! You're on the list.

Naming Ruby Gems

I created a gem called tiedye and I wanted to explain a bit of my reasoning behind why I named it that. So, lets talk about naming. Why call this gem tiedye? How do I decide to name Ruby gems in the first place?

First, there are a few community conventions on how to name your gem. I went with the convention of using underscores in the name of the gem (snake_case instead of camelCase). This applies to all gems that have more than two words as their name. So tiedye or rails are one word but will_paginate and delayed_job are two words written in snake_case. Also I suggest you try to find a fun name for your gem. Ruby has a xml parser named nokogiri, which is Japanese for chainsaw and I named my gem that works with colors tiedye because of the mix of colors those awesome hippies love to wear. There are a whole list of good suggestions that you can find here:

Other rules (which you will find in the previous link) are:

  • Use dashes for extensions
  • Mix underscores and dashes appropriately
  • Don’t use UPPERCASE letters

If you don’t understand those then you should have read the article. Seriously. I don’t want to explain it. Go read the article.

The second thing I did is I went to and search for my desired gem name. With so many gems built to date there is a good chance that someone else already has used that name. You want your gem to have a unique name so that people can remember it and so that you can deploy it to Don’t worry, though there are plenty of names still available. Of course, all this only applies if you are trying to make a public gem. If you are making a private gem, then you will be requiring it from a private repo on github and you can name it what you like. Though you might still conflict with other named gems if you decide to name it something like “rails”.

Also for a good read check out:

Success! You're on the list.