From: Brent Dillingham Date: 2008-08-24T01:05:36+09:00 Subject: Re: remove_const, Kernel.load, and already instantiated objects Thanks for the replies! Botp, your "cheat" does indeed allow you to use require again to load the source file, but this is actually doing the same thing as using Kernel.load. It's just interpreting the contents of foo.rb again, effectively reopening class Foo. Kernel.load is just like require, only require checks $LOADED_FEATURES I guess before it blindly interprets the content of your file again. Kernel.load doesn't do any such check; it interprets the file you point it to no matter what. I did try it, but unfortunately DataMapper still complains about missing properties and such on existing objects after the reload. And the problem still remains that we are not truly "reloading" the class ... we're just reopening it, adding or overwriting methods, just like we might in an IRB session. I did try to go the Marshal route, but Marshal.dump chokes when trying to dump most of my objects with "TypeError: can't dump hash with default proc". I don't know exactly what that's referring to (the stack trace is useless), but it probably has something to do with a DataMapper feature. Marshal seems to be pretty unreliable for any kind of non-trivial object, and I think doing any kind of "deep copy" on my objects will lead to weird duplication issues with their associations anyway. e.g. two Player objects in the same Room need to refer to the same object_id for player.room (at least, that's the assumption I'm designing under right now to prevent hitting the DB constantly). If I do a deep copy as Marshal.dump(player) would do, I'll end up with the players referring to two brand new, different copies of Room objects. Which is bad, I think. Though it's possible that I'm making a mistake by relying on the Room objects staying in-memory. I need to read up on how garbage collection works. At any rate, I'm about to give up on this idea unless anyone has any other suggestions. Thanks! Brent On Aug 23, 2008, at 12:05 AM, Peņa, Botp wrote: > From: Brent Dillingham [mailto:brentdillingham@gmail.com] > # ... > # >> puts File.read('foo.rb') > # class Foo > # def hello > # "Hello, world!" > # end > # end > # => nil > # >> require 'foo' > # => true > > ok. you used require > > # # Reload the file using remove_const and Kernel.load > # >> Object.send(:remove_const, :Foo) > # => Foo > # >> Kernel.load('foo.rb') > # => true > > now you use load. > > let us stick to require... ;) > > irb(main):001:0> require 'foo.rb' > => true89 > irb(main):002:0> f=Foo.new > => # > irb(main):003:0> f.hello > => "hello" > > ok > > irb(main):004:0> require 'foo.rb' > => false > irb(main):005:0> f.hello > => "hello" > > as expected, no change. > let's see if we can cheat ;) > > irb(main):006:0> $LOADED_FEATURES > => ["e2mmap.rb", "irb/init.rb", "irb/workspace.rb", "irb/ > context.rb", "irb/extend-command.rb", "irb/output-method.rb", "irb/ > notifier.rb", "irb/slex.rb", "irb/ruby-token.rb", "irb/ruby-lex.rb", > "readline.so", "irb/input-method.rb", "irb/locale.rb", "irb.rb", > "irb/ext/history.rb", "irb/ext/save-history.rb", "foo.rb"] > > ah > > irb(main):007:0> $LOADED_FEATURES.delete "foo.rb" > => "foo.rb" > > irb(main):008:0> require 'foo.rb' > => true > irb(main):009:0> f.hello > => "new hello" > > is that ok? > ... caveat, i'm not sure if that is documented/supported. maybe, > verify fr matz or nobu.. > > kind regards -botp > >