DEV Community

Cover image for From Hours to Seconds: Simplifying RuboCop with rubocop-hk
Hammad Khan
Hammad Khan

Posted on

From Hours to Seconds: Simplifying RuboCop with rubocop-hk

The two-hour setup that drove me crazy

I started what should have been a simple task last Thursday at 3 PM: setting up RuboCop for a new Ruby project. I was still changing configuration files, searching for "best RuboCop rules for Ruby 3.2," and arguing with myself about whether to use double or single quotes by 5 PM. Does this sound familiar?

At that point, I knew it was time to stop. If I'm spending two hours setting up a linter instead of writing code, something is wrong. That night, I began working on rubocop-hk, a pre-configured RuboCop gem that lets you start linting in just 30 seconds.

What is rubocop-hk?

rubocop-hk is a RuboCop configuration gem that works well right away without any changes. It's like RuboCop's opinionated cousin who has already made all the hard choices for you.

RuboCop's default setup takes a long time to customize, but rubocop-hk gives you a setup that has been tested in real-world Ruby projects and improved over time. You can now install it just like any other gem because it's on RubyGems.

What I like best is It's still RuboCop on the inside. You get all the power of RuboCop without having to worry about setting it up. It saves time by making the most boring part of setting up a project a one-minute task.

The problem it solves (or why I made it)

Let's be honest about what adding RuboCop to a project does:

Before rubocop-hk:

  1. Put RuboCop in your Gemfile (5 minutes)
  2. Run it and get 500 or more offenses (10 minutes of crying)
  3. Begin making .rubocop.yml (15 minutes)
  4. Search for "best RuboCop configuration" on Google (30 minutes)
  5. Take rules from different blog posts (20 minutes)
  6. Talk with your team for 45 minutes about how long the lines should be.
  7. Finally, choose a configuration (15 minutes)
  8. Remember that you forgot to set up Rails cops (start over)

Total time: more than two hours of setting up hell.

After rubocop-hk:

  1. Put rubocop-hk in your Gemfile (30 seconds)
  2. Make a small .rubocop.yml file (30 seconds)
  3. Start coding in a consistent way (worth its weight in gold)

The real killer isn't just the first step. It's keeping things the same across a lot of projects. How many times have you copied .rubocop.yml files from one project to another, only to find that each one is a little different and none of them are quite right?

How to Install and Get Started

Are you ready to take back your afternoon? Here's how to get going:

Step 1: Put it in your Gemfile

group :development, :test do
  gem 'rubocop-hk'
end
Enter fullscreen mode Exit fullscreen mode

Step 2: Install the bundle

$ bundle install
Enter fullscreen mode Exit fullscreen mode

Step 3: Make your .rubocop.yml

inherit_gem:
  rubocop-hk: default.yml

# Optional: Add settings that are specific to your project
AllCops:
  NewCops: enable
  TargetRubyVersion: 3.2
Enter fullscreen mode Exit fullscreen mode

Step 4: Start RuboCop

$ bundle exec rubocop

Looking at 23 files
.......................

23 files checked, no crimes found
Enter fullscreen mode Exit fullscreen mode

That's all. That's really all there is to it. In less than a minute, you've set up RuboCop with settings that are ready for production.

Important Features and Benefits

What makes rubocop-hk special isn't what it adds; it's what it takes away from your workflow:

🎯 The Philosophy of No Configuration

The gem follows the Ruby community's best practices right out of the box. No more being stuck on a decision or making changes over and over. It just works.

⚡ Productivity Right Away

Stop arguing about whether to use %w[] or %w() for arrays of words. Based on popular Ruby style guides and how people use it in the real world, rubocop-hk has already made these choices.

🔧 Still Adjustable

Don't like a rule? You can change it in your .rubocop.yml. rubocop-hk gives you the base, but you are still in charge:

inherit_gem:
  rubocop-hk: default.yml

# Your group likes longer lines
Layout/LineLength:
  Max: 120
Enter fullscreen mode Exit fullscreen mode

📦 Batteries Included

The gem has settings for:

  • Style cops (Ruby idioms that are always the same)
  • Layout cops (spacing and indentation)
  • Lint cops (find real bugs)
  • Metrics cops (code that is easy to understand and change)
  • Naming cops (making sure that variable and method names are always the same)

To see exactly what you're getting, go to GitHub and look at the full configuration.

Examples of How to Use It in Real Life

Example 1: Putting together a Rails project

inherit_gem:
  rubocop-hk: default.yml

require:
  - rubocop-rails

Rails:
  Enabled: true

AllCops:
  Exclude:
    - 'db/schema.rb'
    - 'vendor/**/*'
    - 'config/initializers/*'
Enter fullscreen mode Exit fullscreen mode

Example 2: GitHub Actions CI/CD Pipeline

name: Linters
on: [push, pull_request]

jobs:
  rubocop:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
      - name: Run RuboCop
        run: bundle exec rubocop --parallel
Enter fullscreen mode Exit fullscreen mode

Example 3: Slowly getting the team to use it

Start out strict and then loosen up when you need to:

inherit_gem:
  rubocop-hk: default.yml

# Turn off strict cops for a while while the team gets used to it
Metrics/MethodLength:
  Enabled: false

Style/Documentation:
  Enabled: false
Enter fullscreen mode Exit fullscreen mode

Example 4: Fixing Old Code Automatically

# Fix automatically what's safe to repair
$ bundle exec rubocop -a

# Show what needs to be done by hand
$ bundle exec rubocop --only-recognized-offenses
Enter fullscreen mode Exit fullscreen mode

Who Should Use This?

Developers Who Work Alone

Stop wasting time setting things up. Use rubocop-hk and concentrate on adding new features.

Teams that work on development

Stop arguing about the style guide. Make rubocop-hk your team's standard and get back to solving real problems.

Students in Bootcamp and Ruby Beginners

Learn how to write Ruby code well without having to deal with hundreds of cop configurations.

People who keep open source software up to date

Without having to keep up with complicated documentation, give contributors clear style rules. With rubocop-hk, all you have to do is point them to your .rubocop.yml.

Agencies and Consultants

Keep client projects consistent without having to move configuration files around.

Your turn: Start in 30 seconds

The time is running out. You could have already installed rubocop-hk and be writing better Ruby code by the time you finish reading this conclusion.

  1. Get the gem: rubygems.org/gems/rubocop-hk
  2. Star the repo: github.com/hammadxcm/rubocop-hk
  3. Tell us about your experience: Let us know how long it took you to set up by leaving a comment below!

Do you have any questions? Did you find a bug? Do you have a suggestion for how to set it up? This gem was made for the community, by the community. If you have a problem, open an issue on GitHub.

Stop setting things up. Get started with coding. Your future self will be grateful.


What is your RuboCop setup horror story? Please leave a comment below and let me know how much time you've wasted setting up your linter! 👇

If rubocop-hk helped you save time, please like this post and tell your Ruby friends about it. Let's speed up Ruby development for everyone!

Top comments (0)