rubocop
A Ruby static code analyzer and formatter, based on the community Ruby style guide.
Top Related Projects
Quick Overview
RuboCop is a popular Ruby static code analyzer and formatter. It enforces many of the guidelines outlined in the community-driven Ruby Style Guide, helping developers maintain consistent and clean code across projects.
Pros
- Highly configurable, allowing teams to customize rules to fit their coding style
- Integrates well with various editors and CI/CD pipelines
- Provides auto-correction for many offenses, saving time on manual fixes
- Regularly updated with new features and improvements
Cons
- Can be overwhelming for beginners due to the large number of default rules
- Some rules may be opinionated and not universally agreed upon
- Performance can be slow on large codebases
- Occasional false positives or overly strict enforcement of certain rules
Code Examples
- Basic usage:
# Check a file
rubocop app/models/user.rb
# Check a directory
rubocop app/models
# Auto-correct offenses
rubocop -a
- Disabling a cop inline:
# rubocop:disable Metrics/MethodLength
def long_method
# Method implementation
end
# rubocop:enable Metrics/MethodLength
- Custom configuration in .rubocop.yml:
AllCops:
NewCops: enable
Style/StringLiterals:
EnforcedStyle: double_quotes
Metrics/MethodLength:
Max: 20
Getting Started
- Install RuboCop:
gem install rubocop
- Create a .rubocop.yml file in your project root (optional):
AllCops:
NewCops: enable
TargetRubyVersion: 3.0
- Run RuboCop:
rubocop
- To auto-correct offenses:
rubocop -a
Competitor Comparisons
Static type checker for Ruby
Pros of Steep
- Focuses on static type checking for Ruby, enhancing type safety
- Provides more advanced type inference capabilities
- Supports gradual typing, allowing incremental adoption in existing projects
Cons of Steep
- Narrower scope compared to RuboCop's comprehensive style and lint checks
- Steeper learning curve due to its focus on advanced typing concepts
- Less mature and smaller community compared to RuboCop
Code Comparison
Steep type annotation example:
# @type var x: Integer
x = 1
# @type method foo: (String) -> void
def foo(str)
puts str
end
RuboCop configuration example:
Style/StringLiterals:
Enabled: true
EnforcedStyle: single_quotes
Metrics/LineLength:
Max: 120
Summary
Steep is a specialized tool for static type checking in Ruby, offering advanced type inference and gradual typing. It's ideal for projects prioritizing type safety but has a narrower focus compared to RuboCop. RuboCop, on the other hand, provides a comprehensive suite of style and lint checks, making it more versatile for general Ruby code quality improvement. The choice between the two depends on project needs, with Steep being more suitable for projects emphasizing strong typing, while RuboCop offers broader code style and quality enforcement.
A static analysis security vulnerability scanner for Ruby on Rails applications
Pros of Brakeman
- Focuses specifically on security vulnerabilities in Ruby on Rails applications
- Provides detailed explanations and remediation advice for detected issues
- Can be integrated into CI/CD pipelines for automated security checks
Cons of Brakeman
- Limited to Ruby on Rails applications, while RuboCop covers general Ruby code
- May produce false positives in some cases, requiring manual verification
- Less frequent updates compared to RuboCop
Code Comparison
Brakeman (security-focused):
# Potential SQL injection vulnerability
User.where("name = '#{params[:name]}'")
# Secure alternative
User.where(name: params[:name])
RuboCop (style and best practices):
# Violates RuboCop's style guide
def some_method()
x = 1
y = 2
end
# RuboCop-compliant version
def some_method
x = 1
y = 2
end
Brakeman focuses on identifying security vulnerabilities specific to Rails applications, while RuboCop is a more general-purpose Ruby static code analyzer that enforces style guidelines and best practices. Brakeman is essential for security-conscious Rails developers, while RuboCop is valuable for maintaining consistent code quality across Ruby projects.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Role models are important.
-- Officer Alex J. Murphy / RoboCop
RuboCop is a Ruby static code analyzer (a.k.a. linter
) and code formatter. Out of the box it
will enforce many of the guidelines outlined in the community Ruby Style
Guide. Apart from reporting the problems discovered in your code,
RuboCop can also automatically fix many of them for you.
RuboCop is extremely flexible and most aspects of its behavior can be tweaked via various configuration options.
Working on RuboCop is often fun, but it also requires a great deal of time and energy.
Please consider financially supporting its ongoing development.
Installation
RuboCop's installation is pretty standard:
$ gem install rubocop
If you'd rather install RuboCop using bundler
, add a line for it in your Gemfile
(but set the require
option to false
, as it is a standalone tool):
gem 'rubocop', require: false
RuboCop is stable between minor versions, both in terms of API and cop configuration.
We aim to ease the maintenance of RuboCop extensions and the upgrades between RuboCop
releases. All big changes are reserved for major releases.
To prevent an unwanted RuboCop update you might want to use a conservative version lock
in your Gemfile
:
gem 'rubocop', '~> 1.69', require: false
See our versioning policy for further details.
Quickstart
Just type rubocop
in a Ruby project's folder and watch the magic happen.
$ cd my/cool/ruby/project
$ rubocop
You can also use this magic in your favorite editor with RuboCop's built-in LSP server.
Documentation
You can read a lot more about RuboCop in its official docs.
Compatibility
RuboCop officially supports the following runtime Ruby implementations:
- MRI 2.7+
- JRuby 9.4+
Targets Ruby 2.0+ code analysis.
See the compatibility documentation for further details.
Readme Badge
If you use RuboCop in your project, you can include one of these badges in your readme to let people know that your code is written following the community Ruby Style Guide.
Here are the Markdown snippets for the two badges:
[](https://github.com/rubocop/rubocop)
[](https://rubystyle.guide)
Team
Here's a list of RuboCop's core developers:
- Bozhidar Batsov (author & head maintainer)
- Jonas Arvidsson
- Yuji Nakayama (retired)
- Evgeni Dzhelyov (retired)
- Ted Johansson
- Masataka Kuwabara
- Koichi Ito
- Maxim Krizhanovski
- Benjamin Quorning
- Marc-André Lafortune
- Daniel Vandersluis
See the team page for more details.
Logo
RuboCop's logo was created by Dimiter Petrov. You can find the logo in various formats here.
The logo is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Contributors
Here's a list of all the people who have contributed to the development of RuboCop.
I'm extremely grateful to each and every one of them!
If you'd like to contribute to RuboCop, please take the time to go through our short contribution guidelines.
Converting more of the Ruby Style Guide into RuboCop cops is our top priority right now. Writing a new cop is a great way to dive into RuboCop!
Of course, bug reports and suggestions for improvements are always welcome. GitHub pull requests are even better! :-)
Funding
While RuboCop is free software and will always be, the project would benefit immensely from some funding. Raising a monthly budget of a couple of thousand dollars would make it possible to pay people to work on certain complex features, fund other development related stuff (e.g. hardware, conference trips) and so on. Raising a monthly budget of over $5000 would open the possibility of someone working full-time on the project which would speed up the pace of development significantly.
We welcome both individual and corporate sponsors! We also offer a wide array of funding channels to account for your preferences (although currently Open Collective is our preferred funding platform).
If you're working in a company that's making significant use of RuboCop we'd appreciate it if you suggest to your company to become a RuboCop sponsor.
You can support the development of RuboCop via GitHub Sponsors, Patreon, PayPal, Open Collective and Tidelift .
Note: If doing a sponsorship in the form of donation is problematic for your company from an accounting standpoint, we'd recommend the use of Tidelift, where you can get a support-like subscription instead.
Open Collective for Individuals
Support us with a monthly donation and help us continue our activities. [Become a backer]
Open Collective for Organizations
Become a sponsor and get your logo on our README on GitHub with a link to your site. [Become a sponsor]
Changelog
RuboCop's changelog is available here.
Copyright
Copyright (c) 2012-2024 Bozhidar Batsov. See LICENSE.txt for further details.
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot