text stringlengths 0 444 |
|---|
replace(res) |
end |
def flatten_once |
dup.flatten_once! |
end |
end |
---- |
=== Unused Variables Prefix [[underscore-unused-vars]] |
Prefix with `+_+` unused block parameters and local variables. |
It's also acceptable to use just `+_+` (although it's a bit less descriptive). |
This convention is recognized by the Ruby interpreter and tools like RuboCop will suppress their unused variable warnings. |
[source,ruby] |
---- |
# bad |
result = hash.map { |k, v| v + 1 } |
def something(x) |
unused_var, used_var = something_else(x) |
# some code |
end |
# good |
result = hash.map { |_k, v| v + 1 } |
def something(x) |
_unused_var, used_var = something_else(x) |
# some code |
end |
# good |
result = hash.map { |_, v| v + 1 } |
def something(x) |
_, used_var = something_else(x) |
# some code |
end |
---- |
=== `other` Parameter [[other-arg]] |
When defining binary operators and operator-alike methods, name the parameter `other` for operators with "symmetrical" semantics of operands. |
Symmetrical semantics means both sides of the operator are typically of the same or coercible types. |
Operators and operator-alike methods with symmetrical semantics (the parameter should be named `other`): `+`, `-`, `+*+`, `/`, `%`, `**`, `==`, `>`, `<`, `|`, `&`, `^`, `eql?`, `equal?`. |
Operators with non-symmetrical semantics (the parameter should *not* be named `other`): `<<`, `[]` (collection/item relations between operands), `===` (pattern/matchable relations). |
Note that the rule should be followed *only* if both sides of the operator have the same semantics. |
Prominent exception in Ruby core is, for example, `Array#*(int)`. |
[source,ruby] |
---- |
# good |
def +(other) |
# body omitted |
end |
# bad |
def <<(other) |
@internal << other |
end |
# good |
def <<(item) |
@internal << item |
end |
# bad |
# Returns some string multiplied `other` times |
def *(other) |
# body omitted |
end |
# good |
# Returns some string multiplied `num` times |
def *(num) |
# body omitted |
end |
---- |
== Flow of Control |
=== `for` Loops [[no-for-loops]] |
Do not use `for`, unless you know exactly why. |
Most of the time iterators should be used instead. |
`for` is implemented in terms of `each` (so you're adding a level of indirection), but with a twist - `for` doesn't introduce a new scope (unlike `each`) and variables defined in its block will be visible outside it. |
[source,ruby] |
---- |
arr = [1, 2, 3] |
# bad |
for elem in arr do |
puts elem |
end |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.