fr: Kenneth McDonald [mailto:kenneth.m.mcdonald / sbcglobal.net] :
# Having looked through the Class and Method rdoc, and the Reflection 
# section of Pickaxe, and still not seeing anything (even though it was 
# probably right in front of my nose), I'm forced to fall on 
# the mercy of 
# strangers and ask...how does one add a method to a class while the 
# program is running?

this is just one stupid example,

C:\temp>cat test.rb
puts "i'm running..."

class C
   puts "i'm running... inside class"
end

puts "i'm running..."

c = C.new

puts c.meth rescue puts "no c.meth yet, so this will print"

class C
   puts "i'm running... inside class; creating method meth now"
   def meth
      "yeow!"
   end
end

puts "i'm running..."
puts c.meth rescue puts "meth will run now, so this will not print"

puts "i'm running..."
puts c.meth2 rescue puts "method meth2 not yet defined"

puts "let us put the creation on a string..."
puts "i'm running..."
s = <<CLASS
class C
   puts "i'm running... inside class; creating method second meth2 now"
   def meth2
      "hehey!"
   end
end
CLASS

puts "i'm running..."
eval(s)

puts "now meth2 will run"

puts "i'm running..."
puts c.meth2 rescue puts "meth2 will run, so this will not print"

puts "i'm running..."
puts c.meth3 rescue puts "meth3 will not run yet, so this will print"

puts "now let us put the creation of meth3 using get. i know, but this is a stup
id example"
puts "i'm running..."
puts "pls enter a class definition in one line only:"
s = gets
eval(s)
puts "i'm running..."
puts c.meth3 rescue puts "now meth3 will run, so this will not print"
puts "i'm running..."
puts "ok"

C:\temp>ruby test.rb
i'm running...
i'm running... inside class
i'm running...
no c.meth yet, so this will print
i'm running... inside class; creating method meth now
i'm running...
yeow!
i'm running...
method meth2 not yet defined
let us put the creation on a string...
i'm running...
i'm running...
i'm running... inside class; creating method second meth2 now
now meth2 will run
i'm running...
hehey!
i'm running...
meth3 will not run yet, so this will print
now let us put the creation of meth3 using get. i know, but this is a stupid exa
mple
i'm running...
pls enter a class definition in one line only:
class C; def meth3; "hello from meth3"; end;end
i'm running...
hello from meth3
i'm running...
ok

C:\temp>

kind regards -botp