text stringlengths 0 444 |
|---|
end |
---- |
NOTE: The only valid use-case for the stream constants is obtaining references to the original streams (assuming you've redirected some of the global vars). |
=== Warn [[warn]] |
Use `warn` instead of `$stderr.puts`. |
Apart from being more concise and clear, `warn` allows you to suppress warnings if you need to (by setting the warn level to 0 via `-W0`). |
[source,ruby] |
---- |
# bad |
$stderr.puts 'This is a warning!' |
# good |
warn 'This is a warning!' |
---- |
=== `Array#join` [[array-join]] |
Prefer the use of `Array#join` over the fairly cryptic `Array#*` with a string argument. |
[source,ruby] |
---- |
# bad |
%w[one two three] * ', ' |
# => 'one, two, three' |
# good |
%w[one two three].join(', ') |
# => 'one, two, three' |
---- |
=== Array Coercion [[array-coercion]] |
Use `Array()` instead of explicit `Array` check or `[*var]`, when dealing with a variable you want to treat as an Array, but you're not certain it's an array. |
[source,ruby] |
---- |
# bad |
paths = [paths] unless paths.is_a?(Array) |
paths.each { |path| do_something(path) } |
# bad (always creates a new Array instance) |
[*paths].each { |path| do_something(path) } |
# good (and a bit more readable) |
Array(paths).each { |path| do_something(path) } |
---- |
=== Ranges or `between` [[ranges-or-between]] |
Use ranges or `Comparable#between?` instead of complex comparison logic when possible. |
[source,ruby] |
---- |
# bad |
do_something if x >= 1000 && x <= 2000 |
# good |
do_something if (1000..2000).include?(x) |
# good |
do_something if x.between?(1000, 2000) |
---- |
=== Predicate Methods [[predicate-methods]] |
Prefer the use of predicate methods to explicit comparisons with `==`. |
Numeric comparisons are OK. |
[source,ruby] |
---- |
# bad |
if x % 2 == 0 |
end |
if x % 2 == 1 |
end |
if x == nil |
end |
# good |
if x.even? |
end |
if x.odd? |
end |
if x.nil? |
end |
if x.zero? |
end |
if x == 0 |
end |
---- |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.