bin/rake
Use match?
instead of =~
when MatchData
is not used. Open
Open
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
- Read upRead up
- Exclude checks
In Ruby 2.4, String#match?
, Regexp#match?
and Symbol#match?
have been added. The methods are faster than match
.
Because the methods avoid creating a MatchData
object or saving
backref.
So, when MatchData
is not used, use match?
instead of match
.
Example:
# bad
def foo
if x =~ /re/
do_something
end
end
# bad
def foo
if x.match(/re/)
do_something
end
end
# bad
def foo
if /re/ === x
do_something
end
end
# good
def foo
if x.match?(/re/)
do_something
end
end
# good
def foo
if x =~ /re/
do_something(Regexp.last_match)
end
end
# good
def foo
if x.match(/re/)
do_something($~)
end
end
# good
def foo
if /re/ === x
do_something($~)
end
end
Align the parameters of a method call if they span more than one line. Open
Open
Pathname.new(__FILE__).realpath)
- Read upRead up
- Exclude checks
Here we check if the parameters on a multi-line method call or definition are aligned.
Example: EnforcedStyle: withfirstparameter (default)
# good
foo :bar,
:baz
# bad
foo :bar,
:baz
Example: EnforcedStyle: withfixedindentation
# good
foo :bar,
:baz
# bad
foo :bar,
:baz