This repository was archived by the owner on May 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Ruby Style Guide
moonglum edited this page Oct 17, 2011
·
1 revision
This Styleguide will be a reference for how to format the Ruby Code contributed to this project. This is important, so the entire code looks like "all of a piece". There will soon be some changes here (Ruby 1.9 Hash Syntax f.e.) plus changes to the code, so it matches those guidelines.
Based upon Christian Neukirchen's Styleguide
- Encoding: UTF8 with Unix-style line endings
- Use 2 space indent, no tabs
- Use spaces around operators, after commas, colons and semicolons, around { and before }
- No spaces after (, [ and before ], )
- No spaces before and after default parameters: def method(a=1, b='sth')
- Favor newline over ;
- Use empty lines to break up a long method into logical paragraphs
- Keep lines fewer than 80 characters
- Avoid trailing whitespace
- On top of a file, do:
- (optional) Shebang (#!/usr/bin/env ruby)
- File-header (we need specific documentation for this)
- Module imports, each in alphabetical order:
- Standard-modules, i.e. 'net'
- Newline
- Third party modules/files, i.e. 'rspec'
- Newline
- Files and modules from the current project.
- Use require instead of include unless you really need the include-behaviour.
- Use def with parentheses when there are arguments, use no parenthesis if there are no arguments
- Never use for loops, unless you exactly know why (and I can't think of any case)
- Use &&/|| for boolean expressions, and/or for control flow
- Avoid ?:, use if and unless
- Use {...} block syntax for one-liner, do...end for Multines
- Avoid return where not required
- If you have to fear void or nil, use ||= for a default value
- Never use a logical operator in an unless statement - use if instead
- For class method definitions use Classname.method_name in favor of self.method_name
- Instead of the class << self syntax, define multiple methods as indicated above.
- Use lowercase snake_case for variables and methods
- Use CamelCase for classes and modules
- Use SCREAMING_SNAKE_CASE for constants
- Variables, Getters and Setters should be nouns, methods should be verbs (not single letters)
- Use plural for collections, except for hashes. numbers = [], but person = {}
- Methods that return boolean values have a ? at the end
- Methods that change the object have a ! at the end