From: Xavier Noria Date: 2006-11-12T05:55:49+09:00 Subject: class reloading and constants 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? -- fxn fxn@feynman:~/tmp$ irb irb(main):001:0> IO.read('foo.rb') => "module A\n class B\n def x\n 'x'\n end\n end\nend" irb(main):002:0> load 'foo.rb' => true irb(main):003:0> A::B.new.x => "x" irb(main):004:0> A::B.object_id => 1683700 irb(main):005:0> A.send(:remove_const, 'B') => A::B irb(main):006:0> IO.read('foo.rb') => "module A\n class B\n def y\n 'y'\n end\n end\nend" irb(main):007:0> load 'foo.rb' => true irb(main):008:0> A::B.new.x NoMethodError: undefined method `x' for # from (irb):8 from :0 irb(main):009:0> A::B.new.y => "y" irb(main):010:0> A::B.object_id => 2952610