text
stringlengths
0
444
=== No explicit `.rb` to `require` [[no-explicit-rb-to-require]]
Omit the `.rb` extension for filename passed to `require` and `require_relative`.
NOTE: If the extension is omitted, Ruby tries adding '.rb', '.so', and so on to the name
until found. If the file named cannot be found, a `LoadError` will be raised.
There is an edge case where `foo.so` file is loaded instead of a `LoadError`
if `foo.so` file exists when `require 'foo.rb'` will be changed to `require 'foo'`,
but that seems harmless.
[source,ruby]
----
# bad
require 'foo.rb'
require_relative '../foo.rb'
# good
require 'foo'
require 'foo.so'
require_relative '../foo'
require_relative '../foo.so'
----
=== Avoid `tap`
The method `tap` can be helpful for debugging purposes but should not be left in production code.
[source,ruby]
----
# bad
Config.new(hash, path).tap do |config|
config.check if check
end
# good
config = Config.new(hash, path)
config.check if check
config
----
This is simpler and more efficient.
== Tools
Here are some tools to help you automatically check Ruby code against this guide.
=== RuboCop
https://github.com/rubocop/rubocop[RuboCop] is a Ruby static code analyzer and formatter, based on this style guide.
RuboCop already covers a significant portion of the guide and has https://docs.rubocop.org/rubocop/integration_with_other_tools.html[plugins] for most popular Ruby editors and IDEs.
TIP: RuboCop's cops (code checks) have links to the guidelines that they are based on, as part of their metadata.
=== RubyMine
https://www.jetbrains.com/ruby/[RubyMine]'s code inspections are https://confluence.jetbrains.com/display/RUBYDEV/RubyMine+Inspections[partially based] on this guide.
== History
This guide started its life in 2011 as an internal company Ruby coding guidelines (written by https://github.com/bbatsov[Bozhidar Batsov]).
Bozhidar had always been bothered as a Ruby developer about one thing - Python developers had a great programming style reference (https://www.python.org/dev/peps/pep-0008/[PEP-8]) and Rubyists never got an official guide, documenting Ruby coding style and best practices.
Bozhidar firmly believed that style matters.
He also believed that a great hacker community, such as Ruby has, should be quite capable of producing this coveted document.
The rest is history...
At some point Bozhidar decided that the work he was doing might be interesting to members of the Ruby community in general and that the world had little need for another internal company guideline.
But the world could certainly benefit from a community-driven and community-sanctioned set of practices, idioms and style prescriptions for Ruby programming.
Bozhidar served as the guide's only editor for a few years, before a team of editors was formed once the project transitioned to RuboCop HQ.
Since the inception of the guide we've received a lot of feedback from members of the exceptional Ruby community around the world.
Thanks for all the suggestions and the support! Together we can make a resource beneficial to each and every Ruby developer out there.
== Sources of Inspiration
Many people, books, presentations, articles and other style guides influenced the community Ruby style guide. Here are some of them:
* https://en.wikipedia.org/wiki/The_Elements_of_Style["The Elements of Style"]
* https://en.wikipedia.org/wiki/The_Elements_of_Programming_Style["The Elements of Programming Style"]
* https://www.python.org/dev/peps/pep-0008/[The Python Style Guide (PEP-8)]
* https://pragprog.com/book/ruby4/programming-ruby-1-9-2-0["Programming Ruby"]
* https://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177["The Ruby Programming Language"]
== Contributing
The guide is still a work in progress - some guidelines are lacking examples, some guidelines don't have examples that illustrate them clearly enough.
Improving such guidelines is a great (and simple way) to help the Ruby community!
In due time these issues will (hopefully) be addressed - just keep them in mind for now.
Nothing written in this guide is set in stone.
It's our desire to work together with everyone interested in Ruby coding style, so that we could ultimately create a resource that will be beneficial to the entire Ruby community.
Feel free to open tickets or send pull requests with improvements.
Thanks in advance for your help!
You can also support the project (and RuboCop) with financial contributions via one of the following platforms:
* https://github.com/sponsors/bbatsov[GitHub Sponsors]
* https://ko-fi.com/bbatsov[ko-fi]