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
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 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/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
How do you generate each Rails app with the right Rails version? You don’t have to do this:
There’s an easier way. You can tell RubyGems which version you want to run, right on the command line, with underscores:
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?
You can use all the version strings you know from Bundler:
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:
- Someone has already automated it, or
- 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.