# Get formal parameter names of a method?

**URL:** <https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289>\
**Category:** Ruby\
**Created:** [August 14, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289 "2006-08-14T20:16:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [August 14, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289/1 "2006-08-14T20:16:26Z")

</div>

Is there a way to reflect on a method to get the declard names of the  
parameters?

class Equipment

def install(tool, packaging)  
end

end

I can do this:

m = Equipment.new.method(:install)  
m.arity # =\> 2

I want to somehow find out that the client code has declared the  
parameters named ‘tool’ and ‘packaging’.

Is this possible somehow? I feel like it should be, but I can’t quite  
figure it out.

Thanks!  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289/2 "2006-08-15T14:20:06Z")

</div>

On 8/14/06, Jeff C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to reflect on a method to get the declard names of the  
> parameters?

Not in this version of Ruby. I don’t know if it’s planned.

-austin

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289/3 "2006-08-15T14:20:07Z")

</div>

Jeff C. wrote:

> I can do this:
> 
> m = Equipment.new.method(:install)  
> m.arity # =\> 2
> 
> I want to somehow find out that the client code has declared the  
> parameters named ‘tool’ and ‘packaging’.
> 
> Is this possible somehow? I feel like it should be, but I can’t quite  
> figure it out.

It’s not possible without access to the source code or some major  
interpreter patches.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289/4 "2006-08-15T14:20:09Z")

</div>

On 8/14/06, Jeff C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can do this:  
> Thanks!  
> Jeff

the only way you could do this now would be to use ParseTree:

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

Maybe there will be a built-in way to do this in 2.0?

Phil

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289/5 "2006-08-15T14:20:11Z")

</div>

Not that I know of but:  
def install\_args  
[:tool,:packaging]  
end

m = Equipment.new.method(:install)  
Equipment.new.method(:install\_args)  
#m.arity # =\> 2

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289/6 "2006-08-15T14:20:14Z")

</div>

Phil T. wrote:

> > 
> 
> Phil  
> Its a long shot but as a nasty hack you could read the original source  
> file in and search to locate the “def” of the method and then via a  
> regex extract the names of the arguments. This would likely be very  
> slow so you’d probably want to do it once and for all for all methods  
> of interest. But, as a circular problem, how would you know the names  
> of the methods of interest in advance? Well, that would have to be an  
> assumption I suppose.

Good Luck,

Ken

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289/7 "2006-08-15T14:20:29Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Kenosis [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Is this possible somehow? I feel like it should be, but I can’t quite  
> > > Maybe there will be a built-in way to do this in 2.0?
> > 
> > Phil  
> > Its a long shot but as a nasty hack you could read the original source  
> > file in and search to locate the “def” of the method and then via a  
> > regex extract the names of the arguments. This would likely be very  
> > slow so you’d probably want to do it once and for all for all methods  
> > of interest. But, as a circular problem, how would you know the names  
> > of the methods of interest in advance? Well, that would have to be an  
> > assumption I suppose.

If you’re going to do that you might as well use ParseTree.

Phil

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/get-formal-parameter-names-of-a-method/102289/8 "2006-08-15T14:20:33Z")

</div>

On Aug 14, 2006, at 2:16 PM, Jeff C. wrote:

> Is there a way to reflect on a method to get the declard names of the  
> parameters?  
> […]  
> Is this possible somehow? I feel like it should be, but I can’t quite  
> figure it out.

What is the use case for this sort of thing? Off hand the only reason  
I could see needing this would be for some sort of debugger/ide/  
development  
tool, in which case ParseTree might be a solution.

Gary W.
