text stringlengths 0 444 |
|---|
def some_method(options = {}) |
bar = options.fetch(:bar, false) |
puts bar |
end |
# good |
def some_method(bar: false) |
puts bar |
end |
some_method # => false |
some_method(bar: true) # => true |
---- |
=== Keyword Arguments vs Optional Arguments [[keyword-arguments-vs-optional-arguments]] |
Prefer keyword arguments over optional arguments. |
[source,ruby] |
---- |
# bad |
def some_method(a, b = 5, c = 1) |
# body omitted |
end |
# good |
def some_method(a, b: 5, c: 1) |
# body omitted |
end |
---- |
=== Keyword Arguments vs Option Hashes [[keyword-arguments-vs-option-hashes]] |
Use keyword arguments instead of option hashes. |
[source,ruby] |
---- |
# bad |
def some_method(options = {}) |
bar = options.fetch(:bar, false) |
puts bar |
end |
# good |
def some_method(bar: false) |
puts bar |
end |
---- |
=== Arguments Forwarding [[arguments-forwarding]] |
Use Ruby 2.7's arguments forwarding. |
[source,ruby] |
---- |
# bad |
def some_method(*args, &block) |
other_method(*args, &block) |
end |
# bad |
def some_method(*args, **kwargs, &block) |
other_method(*args, **kwargs, &block) |
end |
# bad |
# Please note that it can cause unexpected incompatible behavior |
# because `...` forwards block also. |
# https://github.com/rubocop/rubocop/issues/7549 |
def some_method(*args) |
other_method(*args) |
end |
# good |
def some_method(...) |
other_method(...) |
end |
---- |
=== Block Forwarding |
Use Ruby 3.1's anonymous block forwarding. |
In most cases, block argument is given name similar to `&block` or `&proc`. Their names have no information and `&` will be sufficient for syntactic meaning. |
[source,ruby] |
---- |
# bad |
def some_method(&block) |
other_method(&block) |
end |
# good |
def some_method(&) |
other_method(&) |
end |
---- |
=== Private Global Methods [[private-global-methods]] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.