>>>>> "A" == Andreas Habel <mail / exceptionfault.de> 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
#<NoMethodError: undefined method `d' for #<C:0x40099da4>>
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
#<NoMethodError: undefined method `d' for #<C:0x40099958>>
x
d
x
d
x
d
svg% 



Guy Decoux