On 2002.09.20, Ryan Davis <ryand / zenspider.com> wrote:
> Is there a better way???? caller(n) returns a string, and I have to 
> parse it for the method name. This is scary at best. I'd much rather 
> have something that gave me either the method instance itself, or some 
> instance representation of the call stack. Something like:
> [...]

Another thing, and this may not only be off-the-wall but also
YAGNI in the highest, but I've often wanted caller(-1).

That's right folks: An object that represents the current method
invocation.

One example of the potential is caller(-1).variables, which could
return a hash representing all the local variables.

This would aid in hacking out some quick ExtractMethod's:

  def some_huge_method
    # a dozen vars here
    # a dozen dozen lines of code mixed in
    some_newly_extracted_method caller(-1).variables
  end


Now, this is surely crossing the wackiness line, but what if you
could /assign/ to these vars?  Then the implementation of
some_newly_extracted_method would look like this:

  def some_newly_extracted_method variables_hash
    caller(-1).variables = variables_hash
    # extracted code here, raw and unchanged.
  end

Then, we'd have the ability to mechanically extract methods, for
free.

But maybe we don't need that because we never leave our code to
get so poorly factored that we'd need something like this.
Cough.

 - Ryan King