DEV Community

Cover image for Great Ruby Gems Most People Haven’t Heard About
Ronak Bhatt for Main Street

Posted on

Great Ruby Gems Most People Haven’t Heard About

What are the best Ruby gems that you can use in your Rails projects?

That’s what you’ll discover in this article!

I’m going to give you 7 gems, but not the same old gems that you’ve seen a million times, I’m going to be sharing with you some gems that are very helpful, but little-known.

But before we do that…


A warning.

I’ve seen developers that pull in a gem for just about everything.

If it remotely sounds like it could be helpful.

Without taking a moment to think if that gem solves the problem they have if it’s the best option, well-maintained & documented, etc.

That’s a mistake.


1. Find Dead Routes To Keep Your Code Clean

As your Rails application grows, you’ll accumulate more & more routes.

You’ll change code & some of these routes become stale.

No longer needed…

But they’ll stay there, in your config/routes.rb, making it harder to manage.

How do you find which routes to delete?

There is a gem called traceroute.

It loads your routes & compares them to the controller actions you’ve defined.

But right now it has two limitations:

  • It doesn’t work with Rails 6
  • It doesn’t detect implicit controller actions

You can find it here.


2. Make Your Migrations Safer So You Can Avoid Problems Rails migrations can cause a lot of problems if done wrong.

For example:

Removing a column can cause problems because Rails has a cache of columns, and running a migration doesn’t reset this cache.

Another example:

If you add an index & you’re running PostgreSQL the whole table gets locked until the operation is complete.

Not good for your users.

Your application won’t be able to respond to requests that need to work with a locked table.

The good news?

You don’t have to remember all of these things.

With the help of the strong_migrations gem, you’ll know when you have one of these unsafe migrations before it makes it into production.

You can find it here.


3. Find Your Slow Tests & Make Them Faster

Slow tests are no fun.

But there are tools to help you find why your tests are slow so you can fix them!

One of these tools is the combination of test-prof + ruby-prof.

Here’s how to use it:

TEST_RUBY_PROF=1 rake
Enter fullscreen mode Exit fullscreen mode

Outputs:

%self       total       self         wait        child       calls   name
 43.21      2.001     2.001     0.000     0.000        1     Kernel#sleep
   2.97      0.184     0.138     0.000     0.046     1640   Array#permutation
   1.39      0.064     0.064     0.000     0.000      144   PG::Connection#async_exec
Enter fullscreen mode Exit fullscreen mode

Here I can clearly see my call to sleep, but this could easily be an API call, reading a big file, a slow SQL query, etc.

Another thing you can do is to use the event profiler.

Btw, Rails 6 added parallel testing, you’ve to enable this in test/test_helpers.rb if you’re upgrading your current project.

4. Find Out Which Code Is Used In Production & Which Isn’t

Let’s finish with another tool to help you improve your code.

It’s called coverband.

If you run this gem in production (low overhead), you’ll get a coverage report of what code is being run.

You can even track unused views!

Alt Text

This can help you make decisions when deleting code & cleaning your project.

Don’t let unused code build up!

You can find it here.

Thanks for reading!
I hope that helps someone. Thanks :).

I’d love to hear thoughts or comments around this. Feel free to email me at ronakabhattrz@gmail.com or hit me up on Twitter, @ronakabhattrz .

Top comments (3)

Collapse
 
fatkodima profile image
Fatko Dima

You may consider github.com/fatkodima/online_migrat... as an alternative to strong_migrations. It is a superset of it feature-wise, but not just suggests in words what to do, but has actual migration helpers, which you can use to do what you want.

It has migrations helpers for:

  • renaming tables/columns
  • changing columns types (including changing primary/foreign keys from integer to bigint)
  • adding columns with default values
  • adding different types of constraints
  • and others

Additionally, it has an internal framework for running data migrations on very large tables using background migrations. For example, you can use background migrations to migrate data that’s stored in a single JSON column to a separate table instead; backfill values from one column to another (as one of the steps when changing column type); or backfill some column’s value from an API.

Collapse
 
johnsalzarulo profile image
John Jacob

Dang great post. Thanks for sharing!

Collapse
 
ronakabhattrz profile image
Ronak Bhatt

Thanks, Man !!