RuboCop | Style/PercentLiteralDelimiters

# RuboCop | Style/PercentLiteralDelimiters

## 概要

RuboCopの「Style/PercentLiteralDelimiters」警告について。

Rubyの % 記法利用時の囲い文字をチェックする。

### 設定値一覧

|設定対象|対象|設定値|内容|デフォルト|
|:-----|:--|:---|:---|:---:|
|PreferredDelimiters|%|()|文字列（式展開可）|○|
|PreferredDelimiters|%Q|()|同上|○|
|PreferredDelimiters|%i|()|シンボルの配列（式展開不可）|○|
|PreferredDelimiters|%q|()|文字列（式展開不可）|○|
|PreferredDelimiters|%r|{}|正規表現|○|
|PreferredDelimiters|%s|()|シンボル|○|
|PreferredDelimiters|%w|()|要素が文字列の配列(空白区切り,式展開不可)|○|
|PreferredDelimiters|%W|()|要素が文字列の配列(空白区切り,式展開可)|○|
|PreferredDelimiters|%x|()|コマンド出力|○|

## PercentLiteralDelimiters

各設定値での検証結果をまとめます。

### 検証プログラム

~~~ruby:percent_literal_delimiters.rb
msg = 'message'

puts %(hoge)
puts %i(hoge)
puts %q(hoge)
puts %Q(hoge)
puts %r{ho/g/e}
puts %s(hoge)
puts %w(hoge hige)
puts %W(hoge#{msg} hige)
puts %x(echo `hoge)

puts %@hoge@
puts %i@hoge@
puts %q@hoge@
puts %Q@hoge@
puts %r@ho/g/e@
puts %s@hoge@
puts %w@hoge hige@
puts %W@hoge#{msg} hige@
puts %x@echo `hoge@
~~~

### 実行結果 デフォルト の場合

~~~yaml:.rubocop.yml
PercentLiteralDelimiters:
  PreferredDelimiters:
    '%':  ()
    '%i': ()
    '%q': ()
    '%Q': ()
    '%r': '{}'
    '%s': ()
    '%w': ()
    '%W': ()
    '%x': ()
~~~

~~~bash
$ rubocop percent_literal_delimiters.rb
Inspecting 1 file
C

Offenses:

percent_literal_delimiters.rb:13:6: C: %-literals should be delimited by ( and )
puts %@hoge@
     ^^^^^^^
percent_literal_delimiters.rb:14:6: C: %i-literals should be delimited by ( and )
puts %i@hoge@
     ^^^^^^^^
percent_literal_delimiters.rb:15:6: C: %q-literals should be delimited by ( and )
puts %q@hoge@
     ^^^^^^^^
percent_literal_delimiters.rb:16:6: C: %Q-literals should be delimited by ( and )
puts %Q@hoge@
     ^^^^^^^^
percent_literal_delimiters.rb:17:6: C: %r-literals should be delimited by { and }
puts %r@ho/g/e@
     ^^^^^^^^^^
percent_literal_delimiters.rb:18:6: C: %s-literals should be delimited by ( and )
puts %s@hoge@
     ^^^^^^^^
percent_literal_delimiters.rb:19:6: C: %w-literals should be delimited by ( and )
puts %w@hoge hige@
     ^^^^^^^^^^^^^
percent_literal_delimiters.rb:20:6: C: %W-literals should be delimited by ( and )
puts %W@hoge#{msg} hige@
     ^^^^^^^^^^^^^^^^^^^
percent_literal_delimiters.rb:21:6: C: %x-literals should be delimited by ( and )
puts %x@echo `hoge@
     ^^^^^^^^^^^^^^

1 file inspected, 9 offenses detected
~~~

### 実行結果 囲い文字を全て'@@' に設定します

~~~yaml:.rubocop.yml
PercentLiteralDelimiters:
  PreferredDelimiters:
    '%':  '@@'
    '%i': '@@'
    '%q': '@@'
    '%Q': '@@'
    '%r': '@@'
    '%s': '@@'
    '%w': '@@'
    '%W': '@@'
    '%x': '@@'
~~~

~~~bash
$ rubocop percent_literal_delimiters.rb
Inspecting 1 file
C

Offenses:

percent_literal_delimiters.rb:3:6: C: %-literals should be delimited by @ and @
puts %(hoge)
     ^^^^^^^
percent_literal_delimiters.rb:4:6: C: %i-literals should be delimited by @ and @
puts %i(hoge)
     ^^^^^^^^
percent_literal_delimiters.rb:5:6: C: %q-literals should be delimited by @ and @
puts %q(hoge)
     ^^^^^^^^
percent_literal_delimiters.rb:6:6: C: %Q-literals should be delimited by @ and @
puts %Q(hoge)
     ^^^^^^^^
percent_literal_delimiters.rb:7:6: C: %r-literals should be delimited by @ and @
puts %r{ho/g/e}
     ^^^^^^^^^^
percent_literal_delimiters.rb:8:6: C: %s-literals should be delimited by @ and @
puts %s(hoge)
     ^^^^^^^^
percent_literal_delimiters.rb:9:6: C: %w-literals should be delimited by @ and @
puts %w(hoge hige)
     ^^^^^^^^^^^^^
percent_literal_delimiters.rb:10:6: C: %W-literals should be delimited by @ and @
puts %W(hoge#{msg} hige)
     ^^^^^^^^^^^^^^^^^^^
percent_literal_delimiters.rb:11:6: C: %x-literals should be delimited by @ and @
puts %x(echo `hoge)
     ^^^^^^^^^^^^^^

1 file inspected, 9 offenses detected
~~~

## 補足

この警告は rubocop -a で修正可能です。

## 参照
%記法について（公式）
http://docs.ruby-lang.org/ja/2.0.0/doc/spec=2fliteral.html#percent

## RuboCopまとめ記事

http://qiita.com/tbpgr/items/edbfecb6a6789dd54f47