As far as standards go PHP has PSR, python has PEP and Ruby has..? Well, ruby doesn't really have an established best practices and standards document like the other two languages. Luckily, we do have two excellent coding guides and a gem little called Rubocop that help fill the gap.
Thanks to the awesome work of Bozhidar Batsov and many talented folks, we have an unofficial Ruby and Rails coding standards documentation. These are great resources for when you have questions concerning object oriented programming design challenges. Additionally, they also built the super useful Rubocop gem for automating style checks.
I suggest that you read through, or at least skim through, some of the suggested standards. These were created by some of the top people in the industry and are really great for becoming a Ruby master. Some of the standards proposed can be directly translated into a Sublime Text configuration file which we will now begin to build.
"You have the right to remain silent." Multiple Sublime configurations
Image property of http://www.telegraph.co.uk/
Since, most developers are more or less polyglot's when it comes to programming languages, separating our different coding standards becomes a must. Fortunately, for us Sublime Text 3 has a nifty feature for creating syntax specific user configuration files.
Under the preferences menu in the Settings - More menu you will see the option for Syntax Specific user configuration. By selecting this option Sublime Text will take your currently active file's code syntax and create a config file specific to it (e.g. JSON.sublime-settings). This file will then only be applicable to files that are also using this language syntax.
Create the ruby specific user configuration by clicking the Syntax Specific - User option and then renaming the file to
Ruby.sublime-settings. Once created we can now start following the proposed Ruby style guide.
"Serve the public trust, protect the innocent, uphold the law." Ruby Proposed Standards
Image property of http://www.salon.com/
This section includes the cherry-picked settings that can be configured in your
Ruby.sublime-settings file. These will help bring your coding environment one step closer to being a highly efficient and up to standards workflow.
"Use UTF-8 as the source file encoding."
Using the UTF-8 file encoding is pretty much a standard for most files.
"Use Unix-style line endings."
Like I mentioned in my other article, How to configure Sublime Text for PSR Standards, utilizing unix line endings helps to keep all developers on the same page when using version control.
"Limit lines to 80 characters."
By setting the ruler to 80 and word wrap to true, we can impose a soft limit of 80 characters on a file.
"End each file with a newline."
"Use two spaces per indentation level (aka soft tabs). No hard tabs."
Unlike the PHP world of 4 spaces, Ruby developers generally use 2 spaces for indentation. I find this leads to more compact and readable code.
"Avoid trailing whitespace."
Another setting that teams of developers will all need to use to avoid git conflicts. Kill the whitespace!
Final Ruby.sublime-settings file
I've listed the completed
Ruby.sublime-settings file for easy copy-pasting. Enjoy.
The Ruby style guide has many more standards and best practices contained within it. Some of which we won't be able to configure via a simple settings file. Good thing we have the awesome gem Rubocop to tell us how to fix our code.
"They'll fix you. They fix everything." Rubocop
Image property of http://geektyrant.com/news/top-10-movie-quotes-from-robocop
In addition to the standards documents, the team behind them also created the Rubocop gem. Rubocop is like the ruby linter on steroids as it churns through your code unapologetically telling you what needs to be fixed according to the unofficial coding standards. Similiar to PHP's PSR-fixer, Rubocop can be setup to run in the background or manually at your leisure.
In order to start using Rubocop on your projects, first you need to install the gem with
gem install rubocop. Once installed, I prefer to use the
Sublimelinter-rubocop Sublime package to handle running Rubocop in the background. As per usual install it via Sublime's package manager system. Finally, with both the gem and package ready to go, add a file called
.rubocop.yml in the root directory of your project. It should contain the following code.
This tells Rubocop to in addition to running the regular Ruby style cops (syntax checkers) to also run the Rails cops as well. You can find additional documentation on Sublimelinter-rubocop here and Rubocop itself documentation here.
"Thank you for your co-operation. Good night."
Image property of http://images.fandango.com/images/fandangoblog/
All Robocop quotes aside, by using the above setup you can prevent some of the common syntax pitfalls with Ruby. In addition to the automated setup reading the two unofficial guides can be a lifesaver in determining what method to use or how to structure an application.
Was there an additional setting I missed? How about another tool or gem you use to help you follow standards? Maybe a completely different standard? I would love to hear about them in a comment below. Justice has been served.comments powered by Disqus