Gems every Rails and Ruby project should use

Photo by Don Agnello from Unsplash

TLDR

Gems that I’ve included in every Rails app and Ruby gem I’ve worked on thus far are the following:

pry-byebug
rspec (or rspec-rails for Rails)
rubocop (or rubocop-rails for Rails)

TSPE

Note: TSPE stands for “Too short, please explain”

In the past few years, I’ve built a handful of Ruby based repos from scratch including Gusto’s Partner Directory as well as our OmniAuth strategy gem. While these projects have ranged from being Rails apps, to Rails engines, to Ruby gems, I’ve consistently used these three gems to lay the groundwork for development. Here’s a short explanation of each gem and how they’ve been essential to each project’s success.

RuboCop

👉 For consistency and linting

The RuboCop gem is like the Marie Kondo of code. It is a static code analyzer and formatter that will call out and fix areas of your code that do not follow specified rules—referred to as “cops”. The default RuboCop rules are based off the Ruby Style guide and range from data structure syntax, to how many spaces per line to use, and to handling trailing commas in arguments. You can easily override the defaults cops, add your own, as well as ignore entire files and directories.

These are the primary commands I use to with RuboCop:

# Show files that are violating rules
> bundle exec rubocop --color --force-exclusion --format simple
# Show files that are violating rules and auto-correct
> bundle exec rubocop --color --force-exclusion --format simple --auto-correct
# Generate "TODO" list of files that break rules
> bundle exec rubocop --auto-gen-config
Does anybody else feel weirdly satisfied after auto-correcting their projects with RuboCop?

RuboCop HQ also provides a RuboCop Rails gem which includes RuboCop’s functionality plus more in the context of Rails. It is useful to setup RuboCop as a build check in CI as well as a pre-commit hook so that you are enforced to have linted code.

Install with: $ gem install rubocop or $ gem install rubocop-rails

RSpec

👉 For testing

Any class you create, controller you modify, or piece of code you generate should be tested. Testing allows you to double check your work, ensures that functionality performs as intended, and keeps up to date documentation for how code operates. In Ruby, I like to use the RSpec gem due to its wide usage, its easy to read documentation, and its transferability between Rails and plain Ruby projects (use the RSpec Rails gem for Rails apps).

Example RSpec test

Install with: $ gem install rspec or $ gem install rspec-rails

Pry-Byebug

👉 For debugging

There will be an inevitable moment when you’ll be running/testing code that does not work as expected and it will be very tricky to pin down why. In these situations, I like to use the Pry-Byebug gem. The interface is colorful, the commands are intuitive and—dare I say—it makes debugging your code fun. One simply needs to add the breakpoint:

binding.pry

within any block of code in order to stop your program and investigate local variables and method calls.

Sample debugging session

Note: I would recommend the pry-byebug gem over the pry gem since it includes more functionality such as stack navigation.

Install with: $ gem install pry-byebug

Other 👌 gems

The gems listed above are staples, but below I’ve listed other useful gems that come in handy when the functionality is needed.

factory_bot_rails ⟶ Generating model records in Rails
faker ⟶ Creating fake data (ex. Faker::Hacker.say_something_smart)
httparty ⟶ Make HTTP requests

My goal is to be helpful ✦ Software engineer at Loom ✦ juliannaroen.com (she / her)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store