LoginSignup
7
4

More than 5 years have passed since last update.

RuboCop | Style/HashSyntax

Posted at

RuboCop | Style/HashSyntax

概要

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

Hashの key/value の記述方法をチェックします。
デフォルトでは、Ruby1.9から追加された

{ key: value }

の記法を有効とします。

設定値一覧

設定対象 設定値 内容 デフォルト
EnforcedStyle ruby19 Ruby1.9で導入された, key: value 形式を有効とする
EnforcedStyle hash_rockets key => value 形式を有効とする --

HashSyntax

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

検証プログラム

guard_clause.rb

ruby19_hash = { key: :value }
fat_arrow_hash = { :key => :value }
print ruby19_hash, fat_arrow_hash

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

.rubocop.yml

※明示的に設定しているが、デフォルト値なので何も設定しなくてもよい

HashSyntax:
  EnforcedStyle: ruby19
$ rubocop hash_style.rb
Inspecting 1 file
C

Offenses:

hash_style.rb:2:20: C: Use the new Ruby 1.9 hash syntax.
fat_arrow_hash = { :key => :value }
                   ^^^^^^^

1 file inspected, 1 offense detected

実行結果 ファットアローを利用した書式を有効にします

.rubocop.yml

HashSyntax:
  EnforcedStyle: hash_rockets
$ rubocop hash_style.rb
Inspecting 1 file
C

Offenses:

hash_style.rb:1:17: C: Always use hash rockets in hashes.
ruby19_hash = { key: :value }
                ^^^^

1 file inspected, 1 offense detected

補足

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

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