# Parameter name reflection

**URL:** <https://www.ruby-forum.com/t/parameter-name-reflection/142152>\
**Category:** Ruby\
**Created:** [July 3, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/parameter-name-reflection/142152 "2008-07-03T21:46:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [July 3, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/parameter-name-reflection/142152/1 "2008-07-03T21:46:54Z")

</div>

If I declare

def foo(a, b)  
end

What code should I use to find the names of the parameters?  
I only see these methods available on a Method object.  
["[]", “arity”, “call”, “to\_proc”, “unbind”]

For instance in PHP I could do this

function foo($a, $b) { }  
$ref = new ReflectionFunction(‘foo’)  
$ref-\>getParameters()[0]-\>getName(); // a  
$ref-\>getParameters()[1]-\>getName(); // b

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [July 3, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/parameter-name-reflection/142152/2 "2008-07-03T22:18:08Z")

</div>

On Jul 3, 2008, at 1:43 PM, Oliver S. wrote:

> ## function foo($a, $b) { } $ref = new ReflectionFunction(‘foo’) $ref-\>getParameters()[0]-\>getName(); // a $ref-\>getParameters()[1]-\>getName(); // b
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

cfp:~ \> cat a.rb

def foo a, b  
p local\_variables  
end

foo 4, 2  
cfp:~ \> ruby a.rb  
[“a”, “b”]

as to why you’d want to do this though - i’ve no idea. why not use  
options?

def foo options = {}  
end

```
foo :a => 4, :b => 2

```

??

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [July 5, 2008, 7:32pm UTC](https://www.ruby-forum.com/t/parameter-name-reflection/142152/3 "2008-07-05T19:32:12Z")

</div>

Oliver S. wrote:

> If I declare
> 
> def foo(a, b)  
> end
> 
> What code should I use to find the names of the parameters?

You could use ParseTree[1](http://parsetree.rubyforge.org/ParseTree/) for that purpose:

require ‘rubygems’  
require ‘parse\_tree’

def foo(a, b)  
end

sexp = ParseTree.translate(self.class, :foo)  
args = sexp.assoc(:scope).assoc(:block).assoc(:args)[1…-1]  
puts args[0] # =\> a  
puts args[1](http://parsetree.rubyforge.org/ParseTree/) # =\> b

HTH, Matthias.

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [July 6, 2008, 1:13am UTC](https://www.ruby-forum.com/t/parameter-name-reflection/142152/4 "2008-07-06T01:13:11Z")

</div>

On Jul 5, 2008, at 2:36 PM, Oliver S. wrote:

> ## end
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

bad idea ihmo - it’ll be almost impossible to map valid http request  
names to ruby vars, for instance you’ll have

‘a var with a space’ #=\> ??

‘a var with a /’ #=\> ??

etc etc

in addition the only way you can do that is with a heavy duty eval  
process (vars set by eval are only available to further evals) which  
makes for a super slow request loop. so this sounds like it’ll end up  
being a huge stack of evals - not metaprogramming - unless i’m  
misunderstanding. can you sketch out an example of what you’d like to  
do for us?

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [July 5, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/parameter-name-reflection/142152/5 "2008-07-05T22:38:59Z")

</div>

Thanks Matthias that looks pretty cool.

@ara.t.howard: this is useful as a meta-programming technique. Here’s an  
example:

# controller implementation

def index\_action(id, style, redirected\_from)  
local\_variables # pre-populated with values from HTTP request params  
end
