I have a class which lives in lib/my_class.rb:
class MyClass
@@val = nil
def self.configure(val)
@@val = val
end
def self.getval
@@val
end
end
In config/initializers/my_class.rb:
MyClass.configure(314)
SOME_VAR = 314
However, if I open a rails console, I see the following result:
MyClass.getval
> nil
SOME_VAR
> 314
MyClass.configure(123)
MyClass.getval
> 123
What's even stranger is that, if I move MyClass.configure(314) into environment.rb, I can run MyClass.getval in a console and it returns 314 as expected. Even still, the rails server will randomly "forget" the value stored, causing me to need to restart the server. I would think that it's reloading the class file, causing it to reset its state.
I did some looking around and couldn't find any other examples of the issue. I am on Rails 6.