RuboCop is a Ruby static code analyzer. Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide.
[~/rubytesting/rubocop]$ cat test.rb
def badName
if something
test
end
end
[~/rubytesting/rubocop]$ rubocop --version 0.26.1 [~/rubytesting/rubocop]$ date martes, 7 de octubre de 2014, 10:22:07 WEST
[~/rubytesting/rubocop]$ rubocop test.rb
Inspecting 1 file
W
Offenses:
test.rb:1:5: C: Use snake_case for methods.
def badName
^^^^^^^
test.rb:2:3: C: Use a guard clause instead of wrapping the code inside a conditional expression.
if something
^^
test.rb:2:3: C: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
if something
^^
test.rb:4:5: W: end at 4, 4 is not aligned with if at 2, 2
end
^^^
1 file inspected, 4 offenses detected
[~/rubytesting/rubocop]$ cat test2.rb def good_name test if something end
[~/rubytesting/rubocop]$ rubocop test2.rb Inspecting 1 file . 1 file inspected, no offenses detected
reek is a tool that examines Ruby classes, modules and methods and reports any
Code Smells
it
)[~/rubytesting/reek]$ ls
Gemfile Gemfile.lock Rakefile dirty.rb
[~/rubytesting/reek]$ cat dirty.rb
class Dirty
# This method smells of :reek:NestedIterators but ignores them
def awful(x, y, offset = 0, log = false)
puts @screen.title
@screen = widgets.map {|w| w.each {|key| key += 3}}
puts @screen.contents
end
end
[~/rubytesting/reek]$ reek dirty.rb
dirty.rb -- 8 warnings:
[1]:Dirty has no descriptive comment (IrresponsibleModule)
[3]:Dirty#awful has 4 parameters (LongParameterList)
[3]:Dirty#awful has boolean parameter 'log' (BooleanParameter)
[5]:Dirty#awful has the variable name 'w' (UncommunicativeVariableName)
[3]:Dirty#awful has unused parameter 'log' (UnusedParameters)
[3]:Dirty#awful has unused parameter 'offset' (UnusedParameters)
[3]:Dirty#awful has unused parameter 'x' (UnusedParameters)
[3]:Dirty#awful has unused parameter 'y' (UnusedParameters)