I've been away from e-mail for a few days. I haven't looked at anyone
elses solution yet.
Here's my initial solution which didn't take long. I expect to refine
this with more function in the next few days.
I split out the function into a separate class
MethodPrompter::Interactor, so as not to polute the "class under
training" with extra methods while allowing for modular extension of
the function.
=== method_prompter.rb ===
module MethodPrompter
class Interactor
def initialize(target_object)
@target_object = target_object
end
def prompt_for_method(symbol, *args)
puts "#{symbol} is undefined"
puts "Please define what I should do (end with
a newline):"
@method_body = []
print ">> "
while line = gets
break if line == "\n"
puts "#{line.empty?} >>#{line}<<"
@method_body << line
print ">> "
end
end
def parms_from(*args)
""
end
def make_method(symbol, *args)
method_string = "def #{symbol.to_s}
#{parms_from(args)}\n" <<
@method_body.join("\n") <<
'end'
@target_object.class.module_eval(method_string,
'line',
-1)
end
end
def method_missing(symbol, *args)
interactor = Interactor.new(self)
interactor.prompt_for_method(symbol)
interactor.make_method(symbol, args)
end
end
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/