LoginSignup
4
3

More than 3 years have passed since last update.

【Rubocop】Use 2 (not 1) spaces for indentation.を解決する

Last updated at Posted at 2020-10-07

Rubocopを導入しエラーを解決していく中で、下記のエラーが結構な数出てました。
「Use 2 (not 1) spaces for indentation.」

調べても日本語の解説記事がほとんどなかったので、備忘録として残してみます。
間違っている点等あれば、ご指摘いただければと思います。

ちなみに、Rubocopの自動修正は効かなかった\(^o^)/

環境

MacOS Catalina
Rails 6.0.3.2
Ruby 2.6.3

Use 2 (not 1) spaces for indentation.とは

Google翻訳にかけてみると、下記のような感じ。
「インデントには2つ(1つではなく)のスペースを使用します。」
つまり、インデントを2つ空けろってことかな?

該当のエラー行

app/controllers/messages_controller.rb:2:1: C: Use 2 (not 1) spaces for indentation.
    before_action :set_room, only: [:create, :destroy]
^
app/controllers/messages_controller.rb:6:2: C: Use 2 (not 1) spaces for indentation.
        if Entry.where(user_id: current_user.id, room_id: @room.id)
 ^
app/controllers/messages_controller.rb:7:3: C: Use 2 (not 1) spaces for indentation.
            @message = Message.create(message_params)
  ^

パッと見問題なさそうに見えるんだけど、スペースが1つ空いていると言えば空いてるなぁ。
もっと他にも同じような行あるんだけどなぁ。。。

例えば、下記の行では何故か上の行だけ怒られてる。

    before_action :set_room, only: [:create, :destroy]
    before_action :set_message, only: [:destroy]

謎 of 謎。

修正してみる

before

    before_action :set_room, only: [:create, :destroy]

after

    before_action :set_room, only: [:create,  :destroy]

直してみるとエラーの数が減った・・・違和感あるけどこれ何かおかしくね??
元に戻してみた結果→エラーが更に減る(笑)

でもエラーが減ったのは該当行ではなく別のエラーみたい。
うーんなぜ減ったのかも分からんし何が正解なんだ。。。

よくよく見るとエラーが出ているのが全て同じファイル内なので、
このファイル特有の問題がなにかあると予想。

解決できた

結論は、「エラーが出ているファイルだけタブでインデントしていた」です。
VSCODEでスペースインデントに変更することで、エラーが消えました。
スクリーンショット 2020-10-07 18.01.58.png
なぜこのファイルだけ違ったのかは分からんが・・・。

終わりに

凡ミスでしたが、エラーメッセージに惑わされてなかなか生活にたどり着けませんでした。
同じように悩んでいる方の役に立てば幸いです!

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