ITの隊長のブログ

ITの隊長のブログです。Rubyを使って仕事しています。最近も色々やっているお(^ω^ = ^ω^)

RuboCopさんに怒られたメモ

スポンサードリンク

概要

  • 意味調べて修正しているので、せっかくだからメモする
  • Rubyなれなさすぎてもあり、めっちゃ怒られた(修正箇所いっぱい)

怒られたメモ

C: [Correctable] Style/MutableConstant: Freeze mutable objects assigned to constants.

定数は freeze してね。はい。

qiita.com

[Correctable] Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.

読めばわかりますね。Hashの最後にもカンマ入れてね。はい。

C: Naming/AccessorMethodName: Do not prefix reader method names with get_.

きびしい〜って思ったけど、アクセサメソッドのルールに引っかかったっぽい

www.rubydoc.info

今回はそうじゃないんだけど。。。いや、広義の意味でそうなのか。しかし、変数名とメソッド名が同じになること多くならないかな?

# ids = get_ids
ids = ids  # 極論こうなる

qiita.com

うーんって思ってたけど、下記記事みて考えを改める。

qiita.com

安直だけど、集めてくる系のメソッドだったので、 collect_ とかつけてみました。

C: [Correctable] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.

複数行のメソッド定義の引数が揃っているかので揃えてとのこと。

# bad
        message = format(BLOG_TEMPLATE,
          title: title,
          body: body,
        )

# good
        message = format(
          BLOG_TEMPLATE,
          title: title,
          body: body,
        )

C: [Correctable] Style/NumericPredicate: Use xxxxx.positive? instead of xxxxx > 0.

はい

C: [Correctable] Rails/TimeZone: Do not use Time.new without zone. Use one of Time.zone.now, Time.current, Time.new.in_time_zone, Time.new.utc, Time.new.getlocal, Time.new.xmlschema, Time.new.iso8601, Time.new.jisx0301, Time.new.rfc3339, Time.new.httpdate, Time.new.to_i, Time.new.to_f instead.

なるほど。なんでふたつあるんやって思ってたけど、 currentRails用でしかもそれ使ったほうがいいと。

qiita.com

C: [Correctable] Layout/SpaceAroundEqualsInParameterDefault: Surrounding space missing in default value assignment.

なるほど

qiita.com

C: [Correctable] Style/BlockDelimiters: Avoid using {...} for multi-line blocks.

なるほど。。。インラインじゃないと駄目的な。

C: [Correctable] Style/TrailingCommaInArrayLiteral: Put a comma after the last item of a multiline array.

配列も一緒

C: [Correctable] Style/TrailingUnderscoreVariable: Do not use trailing _s in parallel assignment. Prefer ...

PythonだとOKだけどだめなのね

www.rubydoc.info