Skip to content
Browse files

more indentation fixes

  • Loading branch information...
1 parent 376cc0a commit 9259e8cb5941cb350529f00ad284ab3f47f79418 Bozhidar Batsov committed
Showing with 99 additions and 99 deletions.
  1. +99 −99 README.md
View
198 README.md
@@ -25,21 +25,21 @@ guide for the benefit of the entire Ruby community.
* No spaces after (, [ and before ], ).
- some(arg).other
- [1, 2, 3].length
+ some(arg).other
+ [1, 2, 3].length
* Indent **when** as deep as **case**.
- case
- when song.name == "Misty"
- puts "Not again!" when song.duration > 120
- puts "Too long!" when Time.now.hour > 21
- puts "It's too late"
- else
- song.play
- end
+ case
+ when song.name == "Misty"
+ puts "Not again!" when song.duration > 120
+ puts "Too long!" when Time.now.hour > 21
+ puts "It's too late"
+ else
+ song.play
+ end
- kind = case year
+ kind = case year
when 1850..1889 then "Blues"
when 1890..1909 then "Ragtime"
when 1910..1929 then "New Orleans Jazz" when 1930..1939 then "Swing"
@@ -50,16 +50,16 @@ guide for the benefit of the entire Ruby community.
* Use an empty line before the return value of a method (unless it
only has one line), and an empty line between defs.
- def some_method
- do_something
- do_something_else
+ def some_method
+ do_something
+ do_something_else
- result
- end
+ result
+ end
- def some_method
- result
- end
+ def some_method
+ result
+ end
* Use RDoc and its conventions for API documentation. Don't put an
empty line between the comment block and the **def**.
* Use empty lines to break up a method into logical paragraphs.
@@ -71,35 +71,35 @@ guide for the benefit of the entire Ruby community.
## Syntax
* Use **def** with parentheses when there are arguments. Omit the parentheses when the method doesn't accept any arguments.
- def some_method
- # body omitted
- end
+ def some_method
+ # body omitted
+ end
- def some_method_with_arguments(arg1, arg2)
- # body omitted
- end
+ def some_method_with_arguments(arg1, arg2)
+ # body omitted
+ end
* Never use **for**, unless you exactly know why. Most of the time iterators should be used instead.
- arr = [1, 2, 3]
+ arr = [1, 2, 3]
- # bad
- for elem in arr do
- puts elem
- end
+ # bad
+ for elem in arr do
+ puts elem
+ end
- # good
- arr.each { |elem| puts elem }
+ # good
+ arr.each { |elem| puts elem }
* Never use **then** for multiline **if/unless**.
- # bad
- if x.odd? then
- puts "odd"
- end
+ # bad
+ if x.odd? then
+ puts "odd"
+ end
- # good
- if x.odd?
- puts "odd"
- end
+ # good
+ if x.odd?
+ puts "odd"
+ end
* Use **when x; ...** for one-line cases.
* Use &&/|| for boolean expressions, and/or for control flow. (Rule
@@ -107,31 +107,31 @@ guide for the benefit of the entire Ruby community.
wrong operators.)
* Avoid multiline ?: (the ternary operator), use **if/unless** instead.
* Favor modifier **if/unless** usage when you have a single-line body.
- # bad
- if some_condition
- do_something
- end
+ # bad
+ if some_condition
+ do_something
+ end
- # good
- do_something if some_condition
+ # good
+ do_something if some_condition
- # another good option
- some_condition && do_something
+ # another good option
+ some_condition && do_something
* Favor **unless** over **if** for negative conditions:
- # bad
- do_something if !some_condition
+ # bad
+ do_something if !some_condition
- # good
- do_something unless some_condition
+ # good
+ do_something unless some_condition
- # another good option
- some_condition || do_something
+ # another good option
+ some_condition || do_something
* Suppress superfluous parentheses when calling methods, but keep them
when calling "functions", i.e. when you use the return value in the
same line.
- x = Math.sin(y)
- array.delete e
+ x = Math.sin(y)
+ array.delete e
* Prefer {...} over do...end for single-line blocks. Avoid using {...} for multi-line blocks. Always use do...end for
"control flow" and "method definitions" (e.g. in Rakefiles and
@@ -139,34 +139,34 @@ guide for the benefit of the entire Ruby community.
* Avoid **return** where not required.
- # bad
- def some_method(some_arr)
- return some_arr.size
- end
+ # bad
+ def some_method(some_arr)
+ return some_arr.size
+ end
- # good
- def some_method(some_arr)
- some_arr.size
- end
+ # good
+ def some_method(some_arr)
+ some_arr.size
+ end
* Avoid line continuation (\\) where not required. In practice avoid using line continuations at all.
- # bad
- result = 1 + \
- 2
+ # bad
+ result = 1 + \
+ 2
- # good
- result = 1 \
- + 2
+ # good
+ result = 1 \
+ + 2
* Using the return value of = is okay:
- if v = array.grep(/foo/) ...
+ if v = array.grep(/foo/) ...
* Use ||= freely.
- # set name to Bozhidar, only if it's nil or false
- name ||= "Bozhidar"
+ # set name to Bozhidar, only if it's nil or false
+ name ||= "Bozhidar"
* Avoid using Perl-style global variables(like $0-9, $`, ...)
@@ -178,28 +178,28 @@ guide for the benefit of the entire Ruby community.
* Use SCREAMING_SNAKE_CASE for other constants.
* The length of an identifier determines its scope. Use one-letter variables for short block/method parameters, according to this scheme:
- a,b,c: any object
- d: directory names
- e: elements of an Enumerable
- ex: rescued exceptions
- f: files and file names
- i,j: indexes
- k: the key part of a hash entry
- m: methods
- o: any object
- r: return values of short methods
- s: strings
- v: any value
- v: the value part of a hash entry
- x,y,z: numbers
+ a,b,c: any object
+ d: directory names
+ e: elements of an Enumerable
+ ex: rescued exceptions
+ f: files and file names
+ i,j: indexes
+ k: the key part of a hash entry
+ m: methods
+ o: any object
+ r: return values of short methods
+ s: strings
+ v: any value
+ v: the value part of a hash entry
+ x,y,z: numbers
And in general, the first letter of the class name if all objects are of that type.
* When using **inject** with short blocks, name the arguments **|a, e|** (mnemonic: accumulator, element)
* When defining binary operators, name the argument "other".
- def +(other)
- # body omitted
- end
+ def +(other)
+ # body omitted
+ end
* Prefer **map** over *collect*, **find** over *detect*, **find_all** over *select*, **size** over *length*. This is not a hard requirement, though - if
the use of the alias enhances readability - it's ok to use it.
@@ -218,17 +218,17 @@ the use of the alias enhances readability - it's ok to use it.
* Avoid long parameter lists (more than 3-4 params).
* Use **def self.method** to define singleton methods. This makes the methods more resistent to refactoring changes.
- class TestClass
- # bad
- def TestClass.some_method
- # body omitted
- end
+ class TestClass
+ # bad
+ def TestClass.some_method
+ # body omitted
+ end
- # good
- def self.some_other_method
- # body omitted
+ # good
+ def self.some_other_method
+ # body omitted
+ end
end
- end
* Add "global" methods to Kernel (if you have to) and make them private.
* Avoid **alias** when **alias_method** will do.

0 comments on commit 9259e8c

Please sign in to comment.
Something went wrong with that request. Please try again.