# Get the name of a method's parameter

**URL:** <https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846>\
**Category:** Ruby\
**Created:** [September 7, 2006, 8:35am UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846 "2006-09-07T08:35:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lehuy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@lehuy](https://www.ruby-forum.com/u/lehuy)\
**Post date:** [September 7, 2006, 8:35am UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846/1 "2006-09-07T08:35:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lehuy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@lehuy](https://www.ruby-forum.com/u/lehuy)\
**Post date:** [September 7, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846/2 "2006-09-07T21:41:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lehuy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@lehuy](https://www.ruby-forum.com/u/lehuy)\
**Post date:** [September 7, 2006, 8:45am UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846/3 "2006-09-07T08:45:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lehuy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@lehuy](https://www.ruby-forum.com/u/lehuy)\
**Post date:** [September 7, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846/4 "2006-09-07T21:55:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lehuy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@lehuy](https://www.ruby-forum.com/u/lehuy)\
**Post date:** [September 7, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846/5 "2006-09-07T23:54:18Z")

</div>

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

> 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 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/](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).

---

<div class="post-metadata">

**Author:** ![lehuy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@lehuy](https://www.ruby-forum.com/u/lehuy)\
**Post date:** [September 8, 2006, 12:34am UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846/6 "2006-09-08T00:34:21Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![lehuy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@lehuy](https://www.ruby-forum.com/u/lehuy)\
**Post date:** [September 8, 2006, 1:26am UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846/7 "2006-09-08T01:26:24Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![lehuy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@lehuy](https://www.ruby-forum.com/u/lehuy)\
**Post date:** [September 8, 2006, 1:22am UTC](https://www.ruby-forum.com/t/get-the-name-of-a-methods-parameter/67846/8 "2006-09-08T01:22:23Z")

</div>

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

> 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. 😉 (And  
there is of course the set\_trace\_func based solution combined with  
reading the source file)
