How to set default attribute value for module class

Until recently, this usage worked fine:

module Demo class << self attr_accessor_with_default :x, "hey" end end 

However, this is no longer the case.

attr_accessor_with_default has been removed and I am left without a hint how to set this attribute by default.

+4
source share
2 answers

For regular instance variables, you simply set the variable to its default value inside initialize. For class instance variables, you can set it inside the class body:

 module Demo class << self attr_accessor :x end @x = "hey" end 
+4
source

The following worked for me ...

 class Demo attr_accessor :x def initialize @x= "hey" end end 

Then it can be called Demo.new.x => hey

0
source

Source: https://habr.com/ru/post/1399952/


All Articles