Bye Bye Moore

猫マンション建築の野望を胸に零細事業主として資本主義の荒波に漕ぎ出したアラサー男の技術メモ

RuboCopで実現する美しく調和のとれたRubyスクリプト

RuboCopはRubyのコーディング規則確認ツールです

導入

  $ gem install rubocop
  $ rbenv rehash
  $ exec $SHELL

つかってみる

以下、ダックタイピングの写経で使ったduck.rbです。

class Duck
  def quack
    puts "Quaaaaaack!"
  end

  def is_duck?
    true
  end

  def feathers
    puts "The duck has white and gray feathers."
  end
end
 
class Person
  def quack
    puts "The person imitates a duck."
  end

  def is_duck?
    nil
  end
 
  def feathers
    puts "The person takes a feather from the ground and shows it."
  end
end
 
def in_the_forest(duck)
  duck.quack
  printf " *The duck test* : %s\n" % [ duck.is_duck? ? "ture" : "false"]
  duck.feathers
end
 
def game
  donald = Duck.new
  john = Person.new
  in_the_forest donald
  in_the_forest john
end
 
game

これをrubocopに掛けてみると...

$ rubocop duck.rb
Inspecting 1 file
C

Offenses:

duck.rb:1:1: C: Missing top-level class documentation comment.
class Duck
^^^^^
duck.rb:3:10: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
    puts "Quaaaaaack!"
         ^^^^^^^^^^^^^
duck.rb:6:7: C: Rename is_duck? to duck?.
  def is_duck?
      ^^^^^^^^
duck.rb:11:10: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
    puts "The duck has white and gray feathers."
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
duck.rb:14:1: C: Trailing whitespace detected.
duck.rb:15:1: C: Missing top-level class documentation comment.
class Person
^^^^^
duck.rb:17:10: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
    puts "The person imitates a duck."
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
duck.rb:20:7: C: Rename is_duck? to duck?.
  def is_duck?
      ^^^^^^^^
duck.rb:23:1: C: Trailing whitespace detected.
duck.rb:25:10: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
    puts "The person takes a feather from the ground and shows it."
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
duck.rb:28:1: C: Trailing whitespace detected.
duck.rb:31:36: C: Favor format over String#%.
  printf " *The duck test* : %s\n" % [ duck.is_duck? ? "ture" : "false"]
                                   ^
duck.rb:31:39: C: Space inside square brackets detected.
  printf " *The duck test* : %s\n" % [ duck.is_duck? ? "ture" : "false"]
                                      ^
duck.rb:31:56: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
  printf " *The duck test* : %s\n" % [ duck.is_duck? ? "ture" : "false"]
                                                       ^^^^^^
duck.rb:31:65: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
  printf " *The duck test* : %s\n" % [ duck.is_duck? ? "ture" : "false"]
                                                                ^^^^^^^
duck.rb:34:1: C: Trailing whitespace detected.
duck.rb:41:1: C: Trailing whitespace detected.

1 file inspected, 17 offenses detected

...こ、これは便利ですね(白目