8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RuboCop | Style/PercentLiteralDelimiters

Last updated at Posted at 2014-07-17

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

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

検証プログラム

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@

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

.rubocop.yml
PercentLiteralDelimiters:
  PreferredDelimiters:
    '%':  ()
    '%i': ()
    '%q': ()
    '%Q': ()
    '%r': '{}'
    '%s': ()
    '%w': ()
    '%W': ()
    '%x': ()
$ 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

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

.rubocop.yml
PercentLiteralDelimiters:
  PreferredDelimiters:
    '%':  '@@'
    '%i': '@@'
    '%q': '@@'
    '%Q': '@@'
    '%r': '@@'
    '%s': '@@'
    '%w': '@@'
    '%W': '@@'
    '%x': '@@'
$ 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まとめ記事

8
8
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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?