This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the ruby category.
Last Updated: 2024-11-21
How does reloading work (e.g. in Rails, pre Zeitwerk):
Before each request (or console command), Rails calls Object#remove_const
for every constant - e.g. for User
, it calls Object.send :remove_const, "User"
Because of this, any caching/storage of a reloaded constant in a place that isn't reloaded (e.g. as a global variable) will cause bugs:
# config/initializers/configure_payment_gateway.rb
# careful: global variable being set
$PAYMENT_GATEWAY = Rails.env.production? ? RealGateway : MockedGateway
Now, even when MockedGateway
gets changed, $PAYMENT_GATEWAY
is stuck with
the original version when the initializer ran. I.e reloading does not work.