I was addicted when I wanted to refer to the value set in initialize in the class method
class Hoge attr_accessor :aa def initialize @aa = 'aa' end def self.bb @aa end end p Hoge.bb # => nil
I haven't set the value because I haven't done Hoge.new, So nil is returned
class Hoge attr_accessor :aa def initialize @aa = 'aa' end def bb @aa end end hoge = Hoge.new p hoge.bb
You can refer to the value by using the instance method (it is natural) When you want to use a value in a class method Let's set the value directly to the instance variable without using initialize.
class Hoge @aa = 'aa' def self.bb @aa end end p Hoge.bb #=> "aa"
Whether to use it in a class method or an instance method I was addicted to learning that I had to be aware of the contents of initialize according to the purpose.
Dedicated every day