From: Daniel Martin Date: 2006-08-19T02:48:28+09:00 Subject: Re: [QUIZ] QAPrototype (#91) Ruby Quiz writes: > [Editor's Note: > > I envision this could actually be handy for prototyping classes IRb. Bonus > points if you can later print the source for all methods interactively defined. > > --JEG2] Yeah, I was thinking about that as I read this quiz - the core of the solution seems obvious, though I'm sure we'll see a bunch of variation. What really seems interesting to me, though, is the different ways you could extend this with little helper methods that are useful when you're writing a prototype method, such as: def warn_caller_obsolete(current_meth,*args) call_line = caller[1] called_meth = caller[0].gsub(/^[^`]*`([^']+)'.*/,'\1') warn "#{call_line}: `#{called_meth}' is obsolete; use `#{current_meth}'" self.send(current_meth,*args) end Then, you can do things like this: irb(main):039:0> def b(f); [[f],f]; end; irb(main):040:0* def a(f); warn_caller_obsolete(:b,f); end; irb(main):041:0* a(9) (irb):41:in `irb_binding': `a' is obsolete; use `b' => [[9], 9] (Say, whatever happened to the call_stack RCR?) Similar helper methods could do things like simply log the call for later (assuming the caller can handle getting "nil" back) or delegate the call along to some other object, possibly while logging the fact. (Okay, that last one is probably not of much use) I can imagine a future ruby IDE that would give the option to pop open an editor when this happened.