DEV Community

Cover image for Ruby Style Guide 💻💎
Edwin Nuñez
Edwin Nuñez

Posted on

5 3

Ruby Style Guide 💻💎

What is Ruby Style Guide?

Ruby is the main language at Shopify. We are primarily a Ruby shop and we are probably one of the largest out there. Ruby is the go-to language for new web projects and scripting.

This Style Guide is the result of over a decade of Ruby development at Shopify. Much of its content is based on Bozhidar Batsov's Ruby Style Guide, adapted to Shopify by many contributors.

Installing Rubocop to Your Project

$ gem install rubocop

Now let’s create a program that can be linted better

name = "Karthik"
puts "Hello #{name}"
Enter fullscreen mode Exit fullscreen mode

Now we save it and run Rubocop on it as shown:

$ rubocop rubocop_example.rb

rubocop spits out some errors as shown

Inspecting 1 file
C

Offenses:

rubocop_example.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
name = "Karthik"
^
rubocop_example.rb:1:8: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
name = "Karthik"
       ^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses auto-correctable
Enter fullscreen mode Exit fullscreen mode

- Create a .yml file in the root and paste this:

AllCops:
  NewCops: enable
  Exclude:
    - 'db/migrate/*.rb'
    - 'config/**/*.rb'
    - 'bin/*'
    - 'spec/rails_helper.rb'
    - 'lib/**/*.rb' #it should be fixed later
    - 'spec/lib/**/*.rb' #it should be fixed later
    - 'spec/requests/**/*.rb' #it should be fixed later
Metrics/BlockLength:
  Exclude:
    - 'Rakefile'
    - '**/*.rake'
    - 'spec/**/*.rb'
    - 'app/admin/**/*.rb'
    - 'db/**/*.rb'
    - 'db/migrate/*.rb'
    - 'config/*.rb'
Documentation:
  Include:
    - 'app/models/**/*.rb'
  Exclude:
    - 'app/models/ability.rb'
    - 'app/models/application_record.rb'
    - 'app/models/concerns/**'
    - 'app/models/filter_client.rb'
    - 'app/models/filter_warehouse.rb'
Metrics/MethodLength:
  Max: 12
Metrics/ClassLength:
  Max: 1000

Enter fullscreen mode Exit fullscreen mode

That's all; Rubocop is now ready to work on your project! 🥳 🎉

Thank you for reading!📒

Guarapo Labs creates digital products that assist people in developing their ideas. Our staff has all of the necessary skills to work on your web and virtual reality game projects. Our commitment to educating our clients on how to provide the finest customer experience with their solutions is reflected in the high quality of our software.

Contact us edwin.nunez@guarapo.dev
Guarapo Labs
edwin-nunez - Overview

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay