fr: Kenneth McDonald [mailto:
[email protected]] :
# Having looked through the Class and Method rdoc, and the Reflection=20
# section of Pickaxe, and still not seeing anything (even though it was=20
# probably right in front of my nose), I'm forced to fall on=20
# the mercy of=20
# strangers and ask...how does one add a method to a class while the=20
# 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 =3D 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 =3D <<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 =3D 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