4
0

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 3 years have passed since last update.

RuboCop::Cop::Style::GlobalStdStream違反になったとき

Posted at

現象

rakeタスクでログ出力させるときに

    logger = Logger.new(STDOUT)

と書いたら RuboCop::Cop::Style::GlobalStdStream rubocoopに怒られた。

C: [Correctable] Style/GlobalStdStream: Use $stdout instead of STDOUT.

対応

ドキュメントを読むと
STDOUT/STDERR/STDIN are constants, and while you can actually reassign (possibly to redirect some stream) constants in Ruby, you'll get an interpreter warning if you do so.

つまり、STDOUTにログを追記していくのって、定数に再割り当てしてるのと同義のようで、インタープリターから警告が出るっぽい。よってグローバル変数 $stdout を使うほうがベター。

    logger = Logger.new($stdout)
4
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?