10 Common Rubocop Catches

(1) Style - MutableConstant

# Bad
ERROR_INVALID_PIN = { message: 'PIN is incorrect.' }

# Good
ERROR_INVALID_PIN = { message: 'PIN is incorrect.' }.freeze

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/MutableConstant


(2) Style - HashSyntax

# Bad
h = { :message => 'PIN is incorrect.' }

# Good
h = { message: 'PIN is incorrect.' }

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/HashSyntax


(3) Style - BracesAroundHashParameters

# Bad
user = User.create({ 
  email: 'mail@company.com', 
  username: 'hello world' 
})

# Good
user = User.create(
  email: 'mail@company.com', 
  username: 'hello world'
)

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/BracesAroundHashParameters


(4) Style - PercentLiteralDelimiters

# Bad
%w(string_one string_two)
%i(symbol_one symbol_two)

# Good
%w[string_one string_two]
%i[symbol_one symbol_two]

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/PercentLiteralDelimiters


(5) Layout - IndentHash

# Bad
h = {
    one: '1'
}

# Good
h = {
  one: '1'
}

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Layout/IndentHash


(6) Layout - CaseIndentation

# Bad
case status
  when 'active'
    x = 1
  else
    x = 2
end

# Good
case status
when 'active'
  x = 1
else
  x = 2
end

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Layout/CaseIndentation


(7) Style - IfUnlessModifier

# Bad
if user.nil?
  head :not_found && return
end

# Good
head :not_found && return if user.nil?

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/IfUnlessModifier


(8) Style - NumericLiterals

# Bad
1000000
1_00_000
1_0000

# Good
1_000_000
1000

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/NumericLiterals


(9) Style - SymbolProc

# Bad
comments.each do |comment|
  comment.delete
end

something.map { |s| s.upcase }


# Good
comments.each(&:delete)

something.map(&:upcase)

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/SymbolProc


(10) Style - EmptyLiteral

# Bad
a = Array.new

h = Hash.new

s = String.new


# Good
a = []

h = {}

s = ''

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/EmptyLiteral


(11) Style - ConditionalAssignment

# Bad
if status == '1'
  x += 1
else
  x += 2
end

case status
when '1'
  x = 1
else
  x = 2
end

# Good
x += if status == '1'
       1
     else
       2
     end

x = case status
    when '1'
      1
    else
      2
    end

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/ConditionalAssignment


(12) Style - EmptyMethod

# Bad
def activate

end

def prank(user)

end

# Good
def activate; end

def prank(user); end

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/EmptyMethod


(13) Style - Next

# Bad
[1, 2].each do |a|
  if a == 1
    puts a
  end
end

# Good
[1, 2].each do |a|
  next unless a == 1
  puts a
end

http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/Cop/Style/Next



AI Summary
Chrome On-device AI 2024-10-04 22:11:32

Share Article