text
stringlengths 0
444
|
|---|
Use `def` with parentheses when there are parameters.
|
Omit the parentheses when the method doesn't accept any parameters.
|
[source,ruby]
|
----
|
# bad
|
def some_method()
|
# body omitted
|
end
|
# good
|
def some_method
|
# body omitted
|
end
|
# bad
|
def some_method_with_parameters param1, param2
|
# body omitted
|
end
|
# good
|
def some_method_with_parameters(param1, param2)
|
# body omitted
|
end
|
----
|
=== Method Call Parentheses [[method-invocation-parens]][[method-call-parens]]
|
Use parentheses around the arguments of method calls, especially if the first argument begins with an open parenthesis `(`, as in `f((3 + 2) + 1)`.
|
[source,ruby]
|
----
|
# bad
|
x = Math.sin y
|
# good
|
x = Math.sin(y)
|
# bad
|
array.delete e
|
# good
|
array.delete(e)
|
# bad
|
temperance = Person.new 'Temperance', 30
|
# good
|
temperance = Person.new('Temperance', 30)
|
----
|
==== Method Call with No Arguments [[method-invocation-parens-no-args]][[method-call-parens-no-args]]
|
Always omit parentheses for method calls with no arguments.
|
[source,ruby]
|
----
|
# bad
|
Kernel.exit!()
|
2.even?()
|
fork()
|
'test'.upcase()
|
# good
|
Kernel.exit!
|
2.even?
|
fork
|
'test'.upcase
|
----
|
==== Methods That are Part of an Internal DSL [[method-invocation-parens-internal-dsl]][[method-call-parens-internal-dsl]]
|
Always omit parentheses for methods that are part of an internal DSL (e.g., Rake, Rails, RSpec):
|
[source,ruby]
|
----
|
# bad
|
validates(:name, presence: true)
|
# good
|
validates :name, presence: true
|
----
|
==== Methods That Have "keyword" Status in Ruby [[method-invocation-parens-keyword]][[method-call-parens-keyword]]
|
Always omit parentheses for methods that have "keyword" status in Ruby.
|
NOTE: Unfortunately, it's not exactly clear _which_ methods have "keyword" status.
|
There is agreement that declarative methods have "keyword" status.
|
However, there's less agreement on which non-declarative methods, if any, have "keyword" status.
|
===== Declarative Methods That Have "keyword" Status in Ruby [[method-invocation-parens-declarative-keyword]][[method-call-parens-declarative-keyword]]
|
Always omit parentheses for declarative methods (a.k.a. DSL methods or macro methods) that have "keyword" status in Ruby (e.g., various `Module` instance methods):
|
[source,ruby]
|
----
|
class Person
|
# bad
|
attr_reader(:name, :age)
|
# good
|
attr_reader :name, :age
|
# body omitted
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.