# Parameter names from reflection?

**URL:** <https://www.ruby-forum.com/t/parameter-names-from-reflection/47885>\
**Category:** Ruby\
**Created:** [November 14, 2005, 10:08pm UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885 "2005-11-14T22:08:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercury542](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mercury542](https://www.ruby-forum.com/u/mercury542)\
**Post date:** [November 14, 2005, 10:08pm UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885/1 "2005-11-14T22:08:22Z")

</div>

Is there a way to use reflection or some other method to return the  
names of  
the parameters a method uses? I know that ‘arity’ will return the  
number of  
parameters, but I’m looking for the names of them.

Thanks in advance,  
Charlie

---

<div class="post-metadata">

**Author:** ![mercury542](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mercury542](https://www.ruby-forum.com/u/mercury542)\
**Post date:** [November 14, 2005, 10:50pm UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885/2 "2005-11-14T22:50:27Z")

</div>

On 11/14/05, Charlie S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to use reflection or some other method to return the names of  
> the parameters a method uses?

No. Why do you need this?

Ryan

---

<div class="post-metadata">

**Author:** ![mercury542](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mercury542](https://www.ruby-forum.com/u/mercury542)\
**Post date:** [November 15, 2005, 8:09am UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885/3 "2005-11-15T08:09:50Z")

</div>

On Nov 14, 2005, at 1:48 PM, Ryan L. wrote:

> On 11/14/05, Charlie S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Is there a way to use reflection or some other method to return  
> > the names of  
> > the parameters a method uses?
> 
> No. Why do you need this?

Actually, ParseTree can give you this information.

[http://rubyforge.org/projects/parsetree](http://rubyforge.org/projects/parsetree)

---

<div class="post-metadata">

**Author:** ![mercury542](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mercury542](https://www.ruby-forum.com/u/mercury542)\
**Post date:** [November 15, 2005, 4:18am UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885/4 "2005-11-15T04:18:16Z")

</div>

If you can get the binding from the start of the method you can look at  
#local\_variables.

T.

---

<div class="post-metadata">

**Author:** ![mercury542](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mercury542](https://www.ruby-forum.com/u/mercury542)\
**Post date:** [November 16, 2005, 6:30pm UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885/5 "2005-11-16T18:30:07Z")

</div>

Thanks for the feedback. I need a way to do this without changing the  
method  
itself. I’m trying to get an external program a list of parameters from  
a  
Ruby source file. I suppose I could write a parse script.

“Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![mercury542](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mercury542](https://www.ruby-forum.com/u/mercury542)\
**Post date:** [November 15, 2005, 12:58pm UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885/6 "2005-11-15T12:58:50Z")

</div>

Trans wrote:

> If you can get the binding from the start of the method you can look  
> at #local\_variables.

IMHO you can’t do that without changing the method. Charlie asked for a  
solution using reflection so I think this is not an option for him.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![mercury542](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mercury542](https://www.ruby-forum.com/u/mercury542)\
**Post date:** [November 17, 2005, 4:21am UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885/7 "2005-11-17T04:21:33Z")

</div>

On 11/16/05, Charlie S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the feedback. I need a way to do this without changing the method  
> itself. I’m trying to get an external program a list of parameters from a  
> Ruby source file. I suppose I could write a parse script.

In that case use ParseTree like Eric suggested. Here is something that  
probably does about what you want (sorry if I ruined your fun, but  
this seemed like an interesting problem):

require ‘enumerator’  
require ‘parse\_tree’

class MethodParams  
attr\_reader :parsed, :methods  
def initialize(klass)  
@parsed = ParseTree.new.parse\_tree(klass)  
@methods = {}  
@parsed[0].select {|i| i.respond\_to?(:[]) and i[0] == :defn}.each do  
|i|  
if i[2][0] == :scope  
@methods[i[1]] = i[2][1][1][1…-1]  
end  
end  
end  
end

if $0 == **FILE**  
if ARGV.length \< 1  
puts “Usage: #$0 ”  
exit(1)  
end  
class\_list = ObjectSpace.enum\_for(:each\_object, Class).to\_a  
ARGV.each do |file|  
require file  
end  
new\_classes = ObjectSpace.enum\_for(:each\_object, Class).to\_a -  
class\_list  
new\_classes.each do |klass|  
m = MethodParams.new(klass)  
m.methods.keys.each do |method|  
puts “The parameters for #{klass}##{method} are  
#{m.methods[method].join(‘,’)}”  
end  
end  
end  
**END**

Depending on your platform, you may have trouble with RubyInline and  
ParseTree. I had to do some hackery on my Windows laptop to get the  
above to work. Note that the above will only show the parameters of  
instance methods on classes inside the required files.

Ryan

---

<div class="post-metadata">

**Author:** ![mercury542](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@mercury542](https://www.ruby-forum.com/u/mercury542)\
**Post date:** [November 17, 2005, 4:57am UTC](https://www.ruby-forum.com/t/parameter-names-from-reflection/47885/8 "2005-11-17T04:57:40Z")

</div>

RDoc parses this info too, maybe you could dig around in RDoc and see  
how they do it?
