The Pessimize Gem - Assigning a version to all gems in Gemfile

It’s always a good idea to assign versions to all you gems in your Gemfile. Personally I’d say it’s absolutely mandatory and not assigning at least a major version should throw an error, but that’s not up to me which is probably for the best.

But what if you’ve just realised you need to assign versions to everything and you’re too lazy to do it by hand so you want something to do it for you?

That’s where Pessimize comes in.

Pessimize

Pessimize is a fantastic gem that provides a command line tool to automatically assign a pessimistic version constraint to your gems i.e. ~> x.y. It does this by traversing your Gemfile.lock for your currently installed version and updating your Gemfile to constrain you pessimistically to the corresponding minor version.

For example, if you have version 1.2.3 of a gem installed and you run pessimize it will update your Gemfile for that gem to require ~> 1.2. This way you can update your gem without worrying about your major version changing.

Installing

To install either run gem install pessimize or add gem 'pessimize' to your gem file and run bundle install.

Next run pessimize in your terminal. It will automatically update your Gemfile and Gemfile.lock to include pessimistic minor versions of all your gems. It will also back these files up at Gemfile.backup and Gemfile.lock.backup.

No backups

Want to just do it with no backups because you’ve got version control and your diffs can handle that?

Just run pessimize --no-backup. It’ll run your updates and not create backup files. I use this whenever I use pessimize with Guard.

Pitfalls - It overrides your existing versions

This is the only downside and why you should always (always) review changes to your Gemfile before committing them.

Sometimes you need an exact version of a gem so you’ve specified it exactly. Pessimize will override that with a pessimistic version, it won’t pay attention to your existing versions. So if you’ve got 1.2.3 set for a gem because you need exactly 1.2.3, pessimize will override this with ~> 1.2. Watch out for this if you have any of these requirements.

Thanks for subscribing!
Like what I'm doing? Subscribe and I'll let you know when I write new stuff.
Want to unsubscribe?
Subscribe