I have class X that takes much time to initialize itself. I want to make that class singleton and force its creation when rails application starts.
I’ve made the singleton:
class X
@@instance = nil
def self.instance
if @@instance.nil?
@@instance = X.new
puts 'CREATING'
end
return @@instance
end
private_class_method :new
end
The problem is that every time I use this class I see ‘CREATING’ in logs. I’ve tried to put creation of class in initializers directory but it doesn’t work either.
The problem was that Rails in development environment doesn’t cache classes and application code is reloaded every request.