DEV Community

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

Posted on

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

Top comments (0)