Post

`Style/ClassVars` でクラス変数の誤りを指摘される

解決

instance_variable_get メソッドを使用する。

class A
  @client = SomeClient.new
  def do_api
   client.do
  end

  def client
    self.class.instance_variable_get(:@client)
  end
end

ruby - How to correct Style/ClassVars in RuboCop? - Stack Overflow

事象

class A def do_api client.do end def client @@client ||= SomeClient.new end end

のように、クライアントを一度きりしか生成しないように作成していた。

しかし、Rubocopを実行すると、 Style/ClassVars が発生する。 @@xxxx を使用しては駄目のよう。

This post is licensed under CC BY 4.0 by the author.