I have an instance of singleton class SingletonClass within module Modulename
Modulename::SingletonClass.instance
which has a hash @hashname. I have a method on SingletonClass that adds new keys to @hashname.
When I add a new key to @hashname, I can see the new key exists by doing puts @hashname in the controller, but when I do it in SingletonClass, it seems that the new key is not added. Why is that? Why am I able to see the change in the @hashname from controller but not from the singleton class?
Here is a code that reproduces the behaviour I'm trying to describe :
module MyModule
module SubModule
class SingletonClass
include Singleton
def initialize
@items = {}
@items = MyMode.all.map{|c| {c.name => c.secondary_name}}.reduce(:merge)
end
def add_new_item(name, secondary_name)
@items[name] = secondary_name
end
def do_something
@items.each do |k,v|
ap "#{k} => #{v}"
end
end
def another_method
do_something
end
end
end
end
When I do this from my controller :
singleton = MyModule::SubModule::SingletonClass.instance
singleton.add_new_item('test', 'test1')
Then this also from controller :
singleton.do_something
The new item gets printed out so its good.
But when I invoke another_method from my within my singleton class, the new item appears not to be added