最近Rubocopを使いはじめたので、使い方とか警告の意味とかをメモφ(..)
Rubocopとは
RubocopはOSSで開発されているソースコードの静的解析を行うgemです。
RubyStyleGuidにソースコードが準拠しているかチェックすることが出来ます。
Githubリポジトリ:https://github.com/bbatsov/rubocop
実際の開発現場でも使われているので、普段の勉強から使っていき綺麗なコードを書く習慣を身に着けたい…!
Rubocopの使い方
インストール
$ gem install rubocop
使い方
ファイルの検証
$ rubocop hoge.rb
rubocopの警告のON / OFFを独自に設定する
rubocopの警告を独自にON / OFFを設定するためには、.rubocop.yml
をプロジェクトのroot
ディレクトリに配置します。
下記のような形で、.rubocop.yml
に記載することで独自にON / OFFを切り替えることが出来ます。
# クラスにコメントを残さない
Documentation:
Enabled: false # falseにすることでcheckを無効化
警告欄
文字 |
意味 |
. |
Clean(問題なし) |
C |
Convention(慣習違反) |
W |
Warning |
E |
Error |
F |
Fatal |
Inspecting 1 file
C
Offenses:
hoge.rb:28:1: C: Missing top-level class documentation comment.
class Hoge
^^^^^
1 file inspected, 1 offenses detected
警告メッセージMemo
Extra empty line detected at class body end.
google翻訳
クラス本体の最後で余分な空行が検出されました。
class Hoge
def fuga
return piyo
end
end
Final newline missing.
google翻訳
最終改行がありません。
class Hoge
def fuga
return piyo
end
end
1 trailing blank lines detected.
google翻訳
1つの末尾空白行が検出されました。
class Hoge
def fuga
return piyo
end
end
Space missing after comma.
google翻訳
コンマの後にスペースがありません。
Hoge.new(a,b,c)
Hoge.new(a, b, c)
Trailing whitespace detected.
google翻訳
末尾の空白が検出されました。
class hoge
def fuga
puts piyo
end
end
Missing top-level class documentation comment.
google翻訳様
トップレベルのクラスのドキュメントコメントがありません。
class hoge
def fuga
puts piyo
end
end
Use only ascii symbols in comments.
google翻訳
コメントにはASCII記号のみを使用してください。
puts piyo
Space inside { missing. / Space inside } missing.
google翻訳
内側にスペースがありません。
{ hoge: "hoge", fuga: "fuga" }
Line is too long. [84/80]
google翻訳
ラインが長すぎます。 [84/80]
puts HogeHoge.new(hogehoge: 52, fugafuga: 11, piyopiyo: piyo.new("hiyoco", "niwatori")).hogefugapiyo_method
hoge = HogeHoge.new(
hogehoge: 52,
fugafuga: 11,
piyopiyo: piyo.new("hiyoco", "niwatori")
)
puts hoge.hogefugapiyo_method
Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument.
google翻訳
中括弧を最初の引数とは別の行に置いているときに、最後の引数の後の行には、メソッド呼び出し中括弧を閉じる必要があります。
Hoge.new(
hoge: "hoge",
fuga: "fuga")
Hoge.new(
hoge: "hoge",
fuga: "fuga"
)
Inconsistent indentation detected.
google翻訳
矛盾したインデントが検出されました。
private
def hoge
end
private
def hoge
end
Use %i
or %I
for an array of symbols.
google翻訳
シンボルの配列には %i
または%I
を使います。
before_action :set_page, only: [:show, :edit, :update, :destroy]
before_action :set_page, only: %i[show edit update destroy]
おわりに
また新しいメモや警告があれば追記していきます!φ(..)
参考ページ
wonderwall.hatenablog.com
blog-ja.sideci.com