Andy from Webcrunch

Subscribe for email updates:

Portrait of Andy Leverenz
Andy Leverenz

July 23, 2022

Last updated November 5, 2023

Rails Quick Tips - Bundler Magic Tricks

When it comes to leveraging other gems and dependencies in Ruby, Bundler is the primary tool all Rails developers leverage.

Most of us know basic Bundler commands, but you can use additional options to make your developer experience even more pleasant. This Rails quick tip explores some of those additional options.

Automate adding new gems

Adding new gems can be done in multiple ways with Rails. A shortcut I've used is running the bundle add command. With this command, you can add the latest version of a gem you want to install automatically.

bundle add stripe

For example, adding the Stripe gem above can be done with three words on the command line. This triggers bundler to fetch the latest version of the Stripe gem, install it, and add the dependency to your.Gemfile.

My only gripe with this approach is the newly added gem is always appended to the end of the.Gemfile. I'll often organize the file alphabetically, so this requires manual intervention. It's not a big deal, but I consider this one point against this approach.

Denote which environment your gem should be used

Piggybacking on the bundle add command is a plethora of additional options you can use. Sometimes you only need a gem for development and purposes. In this case, you can append an --group (-g for shorthand) to the command to add a gem for a specific environment.

bundle add annotate --group=development

Here I added the annotate gem to the development group. Again my main gripe with this appprimarilyis primarily out of personal preference. Bundler adds the following to the bottom of the.Gemfile.

gem "annotate", :group => :development

I prefer a different syntax and order, so I'ten just add gems manually if I need something segregated by the environment.

Skip installing gems

If, for some reason, you want to add gems to your Gemfile but forgo installation, you can do this with a one-liner.

bundle add name_of_person devise --skip-install

Here I am telling Bundler to install both the name_of_person and devise gems and skip the installation step using the --skip-install command.

This might come in handy if you're making a new template or something that tends to get reused later.

Inspect your favorite gems

When working with Ruby, you might be leveraging a version manager. I use rbenv, but many use rvm. Depending on the version of Ruby you have installed, it's often a chore to manually inspect gems you install to your system.

Instead of hunting down those directories, Bundler gives us an excellent and straightforward interface to inspect our favorite gems quickly.

bundle open stripe

You can inspect a gem you have already installed using the bundle open command. This is handy to explore and understand the inner workings of the gem and perhaps contribute to them. If I'm not sure of available methods of a gem or need to diagnose an issue, I'll sometimes inspect the source code instead of sifting through documentation. This is especially handy for the rails gem as well. The source code of rails is well documented and can help you get out of any stand-still moments as you develop your apps.

Link this article
Est. reading time: 3 minutes
Stats: 943 views

Categories

Collection

Part of the Rails Quick Tips collection

Products and courses