LoginSignup
16
17

More than 5 years have passed since last update.

RuboCopでRubyのコーディングスタイルをチェックする

Posted at

はじめに

RuboCopで、Rubyのコーディングスタイルをチェックする方法を記述します。

環境

  • CentOS 6.5
  • Ruby 2.1.2p95
  • RuboCop 0.26.1
  • vim 7.2.411
  • Syntastic 3.5.0-40

rubocopでコーディングスタイルをチェックする

  • rubocopのインストール

bundlerでインストールします。

Gemfile
gem 'rubocop'
$ bundle
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Installing ast 2.0.0
Installing slop 3.6.0
Installing parser 2.2.0.pre.4
Installing astrolabe 1.3.0
Installing powerpack 0.0.9
Installing rainbow 2.0.0
Installing ruby-progressbar 1.6.0
Installing rubocop 0.26.1
Using bundler 1.7.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

$ rbenv rehash
$ rubocop -v
0.26.1
  • rubocopでチェックする
hello_world.rb
puts "Hello, World!"
$ rubocop hello_world.rb
Inspecting 1 file
C

Offenses:

hello_world.rb:1:6: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
puts "Hello, World!"
     ^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
  • formatを変更して、チェックする
$ rubocop --format clang hello_world.rb
hello_world.rb:1:6: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
puts "Hello, World!"
     ^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
$  rubocop --format fuubar hello_world.rb
hello_world.rb:1:6: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
puts "Hello, World!"
     ^^^^^^^^^^^^^^^
 1/1 file |======================= 100 =======================>| Time: 00:00:00

1 file inspected, 1 offense detected
$ rubocop --format emacs hello_world.rb
/home/admin/work/src/ruby/hello_world.rb:1:6: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
$ rubocop --format simple hello_world.rb
== hello_world.rb ==
C:  1:  6: Prefer single-quoted strings when you don't need string interpolation or special symbols.

1 file inspected, 1 offense detected

修正します。

hello_world.rb
puts 'Hello, World!'
$ rubocop hello_world.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

Syntasticでファイルの更新時にRuboCopでのチェックを行うようにする

  • NeoBundleでプラグインの管理をしている場合
~/.vimrc
NeoBundle 'scrooloose/syntastic'
" RubyのチェックをRuboCopで行います。
let g:syntastic_ruby_checkers = ['rubocop']

" ファイルのオープン時もチェックを実行します。
" 保存時のみチェックをする場合は、0を指定します。
let g:syntastic_check_on_open = 1

" 「mode」にpassiveを指定すると、「:SyntasticCheck」を実行したときのみチェックが実行されます。
" 「mode」にpassiveを指定し、あわせて「active_filetypes」を指定することで、例外条件を設定します。
" 以下のように指定すると、Rubyの場合のみファイルの保存時(オープン時)に自動でチェックが実行されます。
let g:syntastic_mode_map = { "mode": "passive",
                        \"active_filetypes": ["ruby"],
                        \"passive_filetypes": [] }

参考

16
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
17