I am trying to initialize singleton in ruby. Here is the code:
class MyClass
attr_accessor :var_i_want_to_init
@@instance = MyClass.new
def self.instance
@@instance
end
def initialize
puts "I'm being initialized!"
@var_i_want_to_init = 2
end
end
The problem is that initialization is never called and therefore singleton is never initialized. I tried calling initialization init, self.initialize, new and self.new. Nothing succeeded. "I initialized" was never printed, and the variable was never initialized when I instantiated
my_var = MyClass.instance
How to set up a single to initialize? Help evaluate
Pachun
source
share