3 Quick Gem Tricks

I recently updated Ruby and upgraded a few projects. And while I did, I found some pretty cool RubyGems features that I didn’t know about before:

When your executables get out-of-date

I used to use rvm to manage Ruby versions. But the last time I set up my machine, I decided to try going without it. You don’t need Gemsets when you have Bundler, and you can use Homebrew to keep Ruby up to date.

This works great, until you update Ruby. And rails new, bundle install and all those other commands break. They’ll point to the old Ruby version, not the one you just installed.

You could fix this by uninstalling and reinstalling each gem one by one. But that’s just crazy. Instead, try gem pristine:

gem pristine --all --only-executables

gem pristine takes a gem, and resets it to the version you originally downloaded. It’s like uninstalling and reinstalling the gem. (This is also helpful if you decided to edit a gem while debugging, and forgot to change it back.)

--all means “all gems”, and --only-executables means “only reset files like /usr/local/bin/rails and /usr/local/bin/bundle”. That is, only fix the scripts you use to run a gem from the command line.

So this command resets files like /usr/local/bin/rails to what they would have been if you uninstalled and reinstalled the gem.

A minute later, you’ll be back to working on your app.

When you need an older version

When I wrote my post on respond_to, I created some tiny apps to learn more about it. I used a few different Rails versions to see how each version dealt with respond_to and respond_with.

How do you generate each Rails app with the right Rails version? You don’t have to do this:

gem install rails -v 4.0.0
rails new respond_to_4.0
gem uninstall rails

gem install rails -v 4.1.0
rails new respond_to_4.1
gem uninstall rails

There’s an easier way. You can tell RubyGems which version you want to run, right on the command line, with underscores:

rails _4.0.0_ new respond_to_4.0
rails _4.1.0_ new respond_to_4.1

Fuzzy gem versions

But in that last section, there’s still a problem. You probably don’t actually want to install 4.0.0. You want the newest version of 4.0, with all the minor updates.

But do you remember what the newest minor version of Rails 4.0 is?

There are lots of ways to look it up. But why look it up, when RubyGems can just do what you want?

gem install rails -v "~>4.0.0"

You can use all the version strings you know from Bundler:

gem install rails -v ">3.1, <4.1"

Useful! Especially if, like me, you hate doing stuff that a computer is better at.

But you don’t have to know all this

When I ran into these problems, I didn’t know there was an easy answer. But you could guess there would probably be one.

These were all situations where you can solve the problem by yourself, but it’d be repetitive and annoying.

And when you find a repetitive, annoying task like this, especially in a well-used project, it means one of two things:

  1. Someone has already automated it, or
  2. Lots of people are hoping you’ll automate it.

So before you do the busywork, dig a little deeper. Investigate a little bit. It’ll be worth your time.

This post was originally sent exclusively to my list. To get more posts like it in your inbox every Friday, sign up here!

Pushing through tutorials, and still not learning anything?

Have you slogged through the same guide three times and still don't know how to build a real app?

In this free 7-day Rails course, you'll learn specific steps to start your own Rails apps — without giving up, and without being overwhelmed.

You'll also discover the fastest way to learn new Rails features with your 32-page sample of Practicing Rails: Learn Rails Without Being Overwhelmed.

Sign up below to get started:

Powered by ConvertKit

Did you like this article? You should read these:

Comments