Context: I am trying to understand how does Rails implement class reloading.
If we reload a .rb file that defines class A::B with Kernel#load we are just reopening the class, so that's poor man's reloading in the sense that redefined methods are updated, but removed methods are still there.
I guess (it is just a guess) that's the reason Rails does black magic with constants in Class#remove_class. I think the key line there is
parent.send :remove_const, basename unless parent == klass
which removes the basename of the class we want to remove from the constants in its parent module, if any. The irb session below suggests that technique triggers a "fresh" reload of the class, but why? A posteriori one would bet a class definition reopens the class if there's already a class object in that constant, and just instantiates a new one otherwise, is that right?
=> "module A\n class B\n def x\n 'x'\n end\n end\nend"
irb(main):002:0> load 'foo.rb'
irb(main):005:0> A.send(:remove_const, 'B')
=> "module A\n class B\n def y\n 'y'\n end\n end\nend"
irb(main):007:0> load 'foo.rb'
NoMethodError: undefined method `x' for #<A:0x59f27c>