text
stringlengths
0
444
End each file with a newline.
TIP: This should be done via editor configuration, not manually.
=== Should I Terminate Expressions with `;`? [[no-semicolon]]
Don't use `;` to terminate statements and expressions.
[source,ruby]
----
# bad
puts 'foobar'; # superfluous semicolon
# good
puts 'foobar'
----
=== One Expression Per Line [[one-expression-per-line]]
Use one expression per line.
[source,ruby]
----
# bad
puts 'foo'; puts 'bar' # two expressions on the same line
# good
puts 'foo'
puts 'bar'
puts 'foo', 'bar' # this applies to puts in particular
----
=== Operator Method Call
Avoid dot where not required for operator method calls.
[source,ruby]
----
# bad
num.+ 42
# good
num + 42
----
=== Spaces and Operators [[spaces-operators]]
Use spaces around operators, after commas, colons and semicolons.
Whitespace might be (mostly) irrelevant to the Ruby interpreter, but its proper use is the key to writing easily readable code.
[source,ruby]
----
# bad
sum=1+2
a,b=1,2
class FooError<StandardError;end
# good
sum = 1 + 2
a, b = 1, 2
class FooError < StandardError; end
----
There are a few exceptions:
* Exponent operator:
[source,ruby]
----
# bad
e = M * c ** 2
# good
e = M * c**2
----
* Slash in rational literals:
[source,ruby]
----
# bad
o_scale = 1 / 48r
# good
o_scale = 1/48r
----
* Safe navigation operator:
[source,ruby]
----
# bad
foo &. bar
foo &.bar
foo&. bar
# good
foo&.bar