From: ts Date: 2003-08-29T18:43:00+09:00 Subject: Re: Can I rehash during execution ? >>>>> "A" == Andreas Habel writes: A> Is there a possibility to reload or rehash loaded classes during A> runtime ? If I implement a new method for a class, I simply want to A> reload the class definition and execute the new method in every A> instance that is still generated without stoping and restarting my A> script. Just try it svg% cat c.rb #!/usr/bin/ruby class C def c puts "c" end end svg% svg% cat b.rb #!/usr/bin/ruby require 'c' c = C.new c.c begin c.d rescue p $! end File.open("c.rb", "a") do |f| f.puts "class C; def d() puts 'd'; end; end" end load 'c.rb' c.d svg% svg% b.rb c #> d svg% svg% cat c.rb #!/usr/bin/ruby class C def c puts "c" end end class C; def d() puts 'd'; end; end svg% A> And another question: A> I marshal some classes and write them to hd. Now I chance some class A> definitions and add some methods. Do the marshaled classes know these A> methods after reload or is there a good solution for catching errors A> if not ? same here : svg% cat c.rb #!/usr/bin/ruby class C def c puts "c" end end svg% svg% cat b.rb #!/usr/bin/ruby require 'c' c = C.new c.c begin c.d rescue p $! end mar = Marshal.dump(C) class C def d puts "d" end def c puts "x" end end c.c c.d Marshal.load(mar) c.c c.d C.new.c C.new.d svg% svg% b.rb c #> x d x d x d svg% Guy Decoux