How to change the default value of a Struct attribute?

后端 未结 6 1373
孤城傲影
孤城傲影 2020-12-17 07:43

According to the documentation unset attributes of Struct are set to nil:

unset parameters default to nil.

Is it po

6条回答
  •  时光说笑
    2020-12-17 08:16

    Just add another variation:

    class Result < Struct.new(:success, :errors)
      def initialize(*)
        super
        self.errors ||= []
      end
    end
    

提交回复
热议问题