Use =~
in places where the MatchData
returned by #match
will not be used. Open
elsif l.match(/^\s*#\s*groomers: (.*)/)
- Read upRead up
- Exclude checks
This cop identifies the use of Regexp#match
or String#match
, which
returns #<MatchData>
/nil
. The return value of =~
is an integral
index/nil
and is more performant.
Example:
# bad
do_something if str.match(/regex/)
while regex.match('str')
do_something
end
# good
method(str =~ /regex/)
return value unless regex =~ 'str'
Use each_key
instead of keys.each
. Open
commands.keys.each { |cmd|
- Read upRead up
- Exclude checks
This cop checks for uses of each_key
and each_value
Hash methods.
Note: If you have an array of two-element arrays, you can put parentheses around the block arguments to indicate that you're not working with a hash, and suppress RuboCop offenses.
Example:
# bad
hash.keys.each { |k| p k }
hash.values.each { |v| p v }
hash.each { |k, _v| p k }
hash.each { |_k, v| p v }
# good
hash.each_key { |k| p k }
hash.each_value { |v| p v }
Use =~
in places where the MatchData
returned by #match
will not be used. Open
if cloud.match(/^#{Regexp.quote(c)}$/i)
- Read upRead up
- Exclude checks
This cop identifies the use of Regexp#match
or String#match
, which
returns #<MatchData>
/nil
. The return value of =~
is an integral
index/nil
and is more performant.
Example:
# bad
do_something if str.match(/regex/)
while regex.match('str')
do_something
end
# good
method(str =~ /regex/)
return value unless regex =~ 'str'
Use =~
in places where the MatchData
returned by #match
will not be used. Open
if l.match(/^\s*#\s*clouds: (.*)/)
- Read upRead up
- Exclude checks
This cop identifies the use of Regexp#match
or String#match
, which
returns #<MatchData>
/nil
. The return value of =~
is an integral
index/nil
and is more performant.
Example:
# bad
do_something if str.match(/regex/)
while regex.match('str')
do_something
end
# good
method(str =~ /regex/)
return value unless regex =~ 'str'
Use each_value
instead of values.each
. Open
MU::Cloud.resource_types.values.each { |attrs|
- Read upRead up
- Exclude checks
This cop checks for uses of each_key
and each_value
Hash methods.
Note: If you have an array of two-element arrays, you can put parentheses around the block arguments to indicate that you're not working with a hash, and suppress RuboCop offenses.
Example:
# bad
hash.keys.each { |k| p k }
hash.values.each { |v| p v }
hash.each { |k, _v| p k }
hash.each { |_k, v| p v }
# good
hash.each_key { |k| p k }
hash.each_value { |v| p v }
Use meaningful heredoc delimiters. Open
EOS
- Read upRead up
- Exclude checks
This cop checks that your heredocs are using meaningful delimiters.
By default it disallows END
and EO*
, and can be configured through
blacklisting additional delimiters.
Example:
# good
<<-SQL
SELECT * FROM foo
SQL
# bad
<<-END
SELECT * FROM foo
END
# bad
<<-EOS
SELECT * FROM foo
EOS
Redundant use of Object#to_s
in interpolation. Open
puts "#{cmd} RETRY #{retries.to_s}".light_red
- Read upRead up
- Exclude checks
This cop checks for string conversion in string interpolation, which is redundant.
Example:
# bad
"result is #{something.to_s}"
Example:
# good
"result is #{something}"
}
at 228, 4 is not aligned with results.keys.sort { |a, b|
at 226, 0. Open
}.each { |cmd|
- Read upRead up
- Exclude checks
This cop checks whether the end keywords are aligned properly for do end blocks.
Three modes are supported through the EnforcedStyleAlignWith
configuration parameter:
start_of_block
: the end
shall be aligned with the
start of the line where the do
appeared.
start_of_line
: the end
shall be aligned with the
start of the line where the expression started.
either
(which is the default) : the end
is allowed to be in either
location. The autofixer will default to start_of_line
.
Example: EnforcedStyleAlignWith: either (default)
# bad
foo.bar
.each do
baz
end
# good
variable = lambda do |i|
i
end
Example: EnforcedStyleAlignWith: startofblock
# bad
foo.bar
.each do
baz
end
# good
foo.bar
.each do
baz
end
Example: EnforcedStyleAlignWith: startofline
# bad
foo.bar
.each do
baz
end
# good
foo.bar
.each do
baz
end