以下の内容はhttps://www.yokoyan.net/entry/2019/03/08/180000より取得しました。


【Rubocop】Use the return of the conditional for variable assignment and comparison.エラーの対処法

f:id:yokoyantech:20190308164352p:plain

はじめに

Rubocopで見慣れない指摘を受けたのでメモ。

バージョン

  • rubocop
    • 0.49.1

エラー内容

変数の比較と代入は、条件式の戻り値を使うべし。

Use the return of the conditional for variable assignment and comparison.

ダメなコード例

私はJavaで育ったので、つい、以下のようなコードを書いてしまう。

if @user.type == Settings.user.user_type.standard
  msg = t('message.hogehoge')
else
  msg = t('message.fugafuga')
end

修正例

rubyのif文は戻り値を返すので、その値を使って代入をする。 確かにコードがスッキリする。

msg = if @user.type == Settings.user.user_type.standard
        t('message.hogehoge')
      else
        t('message.fugafuga')
      end



以上の内容はhttps://www.yokoyan.net/entry/2019/03/08/180000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14