How reloading code works

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-10-12

How does reloading work (e.g. in Rails, pre Zeitwerk):

  # 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.

References