Get the name of a method's parameter

Does anyone know how to use reflection to get the name of a parameter of
a method ?
It is useful for documenting a class and doing editor with code
completion

···

--
Posted via http://www.ruby-forum.com/.

Le Huy wrote:

Does anyone know how to use reflection to get the name of a parameter of a method ?
It is useful for documenting a class and doing editor with code completion

Unfortunately that is currently impossible.

I hope and expect this will be available in the future, but I
don't think it will be soon.

Hal

Le Huy wrote:

Does anyone know how to use reflection to get the name of a parameter of
a method ?

  def meth(argy)
    p local_variables
  end

···

It is useful for documenting a class and doing editor with code
completion

--
Posted via http://www.ruby-forum.com/\.

def meth(argy)

    p local_variables
  end

The problem with this solution is that it does not get the name of the
variable that it was called with, just what it's assigned to (and not
entirely useful, though definitely not useless). I think Le is asking about
how to get the name of the variable where the value is being copied to argy
from.

M.T.

I don't think so, the OP said:

···

On Sep 7, 2006, at 3:53 PM, Matt Todd wrote:

On Sep 7, 2006, at 2:35 AM, Le Huy wrote:

It is useful for documenting a class and doing editor with code
completion

--
Posted via http://www.ruby-forum.com/\.

which leaves me to believe he's interested in the parameter names for the method definition. (It would be silly (well maybe just extreme) to document every callsite).

def meth(argy)

    p local_variables
  end

The problem with this solution is that it does not get the name of the
variable that it was called with, just what it's assigned to (and not
entirely useful, though definitely not useless). I think Le is asking about
how to get the name of the variable where the value is being copied to argy
from.

M.T.

I don't think so, the OP said:

> It is useful for documenting a class and doing editor with code
> completion
>
> --
> Posted via http://www.ruby-forum.com/\.
>

which leaves me to believe he's interested in the parameter names for
the method definition. (It would be silly (well maybe just extreme)
to document every callsite).

You may be right. Well, if so, then your solution works like a charm. If
not, then there's no solution.

M.T.

···

On Sep 7, 2006, at 2:35 AM, Le Huy wrote:

This is an ugly hack, and isn't very pretty in usage, plus it's got
lots of holes I'm sure, but:

module ParameterInspector
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def find_method_declaration(m)
      cat[/def\s+#{m}($|\(.*)/]
    end

    def find_parameter(method_name, arity)
      find_method_declaration(method_name)[/\(.*\)/][1...-1].split(',')[arity]
    end

    def cat
      File.read(self.file)
    end
  end
end

Then to use it, you must write a class that defines a ::file method,
and include it.

class Example

  include ParameterInspector

  def self.file
    __FILE__
  end

  def say(something)
    puts something
  end
end

puts Example.find_parameter :say, 0
"something"

You may be right. Well, if so, then your solution works like a charm. If
not, then there's no solution.

My solution? I had no solutions, just random commentary. :wink: (And there is of course the set_trace_func based solution combined with reading the source file)

···

On Sep 7, 2006, at 6:30 PM, Matt Todd wrote:

M.T.