Created by Sebastian Korfmann / @skorfmann
“Role models are important.”
Officer Alex J. Murphy / RoboCop
RuboCop is a Ruby static code analyzer.
class MyClass
attr_accessor :name
def initalize name
self.name = name
end
def name
name.upcase
rescue
end
def greet
return 'Hello'+name
end
end
rubocop --format offences 1 SpaceAroundOperators 1 RedundantReturn 1 HandleExceptions 1 MethodDefParentheses 1 Documentation
Ruby Example:
# starting point (line is too long)
def send_mail(source)
Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text)
end
# good (normal indent)
def send_mail(source)
Mailer.deliver(
to: 'bob@example.com',
from: 'us@example.com',
subject: 'Important message',
body: source.text
)
end
Rails Example
# bad
validates_presence_of :email
# good
validates :email, presence: true
Add it to your project
#Gemfile
gem 'rubocop'
rubocop --auto-gen-config
#./rubocop-todo.yml
# Offence count: 3
AccessorMethodName:
Enabled: false
# Offence count: 7
# Cop supports --auto-correct.
AlignArray:
Enabled: false
# Offence count: 20
# Cop supports --auto-correct.
# Configuration parameters: EnforcedHashRocketStyle, EnforcedColonStyle.
AlignHash:
Enabled: false
#...
#./rubocop-settings.yml
HashSyntax:
EnforcedStyle: hash_rockets
Style/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space
Style/NumericLiterals:
Enabled: false
Style/SignalException:
Enabled: false
Metrics/ClassLength:
CountComments: false
Max: 150
#...
Violate the Styleguide on purpose:
# rubocop:disable RescueModifier
if (entity rescue nil)
# rubocop:enable RescueModifier