# Getting method parameters in specified order

**URL:** <https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985>\
**Category:** Ruby\
**Created:** [December 13, 2007, 7:46am UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985 "2007-12-13T07:46:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 13, 2007, 7:46am UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/1 "2007-12-13T07:46:19Z")

</div>

Hi,

If I declare a function that expects 3 arguments,

def bar(x, y, z)  
…  
end

and later, if I have the function name in a string, is there a way to  
get the parameter names using reflection in Ruby? Something similar  
to the following PHP snippet,

$a = new ReflectionFunction(“function\_name”);  
$a-\>getParameters();

Thanks in advance,

Bye,

```
-Chintana
```

---

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 13, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/2 "2007-12-13T14:07:23Z")

</div>

I think we cant do more than this:

irb(main):001:0\> def test(a, b, c)  
irb(main):002:1\> end  
=\> nil  
irb(main):003:0\> method(:test).arity  
=\> 3

---

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 13, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/3 "2007-12-13T16:46:04Z")

</div>

On Dec 13, 2007 6:36 PM, Bernardo Monteiro R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I think we cant do more than this:
> 
> irb(main):001:0\> def test(a, b, c)  
> irb(main):002:1\> end  
> =\> nil  
> irb(main):003:0\> method(:test).arity  
> =\> 3

Thanks Bernardo.

Bye,

```
-Chintana

```

---

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 13, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/4 "2007-12-13T18:56:08Z")

</div>

On Dec 12, 11:44 pm, Chintana W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> If I declare a function that expects 3 arguments,
> 
> def bar(x, y, z)  
> …  
> end
> 
> and later, if I have the function name in a string, is there a way to  
> get the parameter names using reflection in Ruby?

There is not; as Bernardo said, Ruby only allows you to check the  
arity of the method.

I had a need for this, too. (I was working on a project that allowed  
developers to provide arbitrary Ruby-based plugins, and the GUI of the  
application needed to expose the methods and their paramters  
visually.)

For that need, I wrote this:  
[http://phrogz.net/RubyLibs/rdoc/classes/DescribeMethods.html](http://phrogz.net/RubyLibs/rdoc/classes/DescribeMethods.html)

---

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 14, 2007, 4:23am UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/5 "2007-12-14T04:23:00Z")

</div>

From: Chintana W. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Sent: Thursday, December 13, 2007 11:40 PM

# To: ruby-talk ML

# Subject: Re: Getting method parameters in specified order

# 

# On Dec 13, 2007 6:36 PM, Bernardo Monteiro R.

# [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

# 

# \> I think we cant do more than this:

# \>

# \> irb(main):001:0\> def test(a, b, c)

# \> irb(main):002:1\> end

# \> =\> nil

# \> irb(main):003:0\> method(:test).arity

# \> =\> 3

not part of ruby itself, but try looking at ruby2ruby

kind regards -botp

---

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 14, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/6 "2007-12-14T13:55:20Z")

</div>

> Forgive the naive thoughts of a ruby ruby…

ruby nuby, I meant. heh

---

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 14, 2007, 9:16am UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/7 "2007-12-14T09:16:39Z")

</div>

On Dec 14, 2007 8:52 AM, Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

[snip]

Thanks Botp and Phrogz, I’ll look into it.

Bye,

```
-Chintana

```

---

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 15, 2007, 12:52am UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/8 "2007-12-15T00:52:20Z")

</div>

Hi~

On Dec 14, 2007, at 4:57 AM, Lloyd L. wrote:

> > get the parameter names using reflection in Ruby? Something similar

You can accomplish this with the Ruby2Ruby gem. Here is a pastie of  
some code that can do this for you:

[http://pastie.caboo.se/128959](http://pastie.caboo.se/128959)

Here is how you can use it:

~/\_merb/merb \> irb -r get\_args.rb

> > class Foo  
> > def hi(a,b,c)  
> > p a,b,c  
> > end  
> > end  
> > =\> nil  
> > Foo.instance\_method(:hi).get\_args  
> > =\> [[:a], [:b], [:c]]

Cheers-

- Ezra Z.  
– Founder & Software Architect  
– removed\_email\_address@domain.invalid  
– [EngineYard.com](http://EngineYard.com)

---

<div class="post-metadata">

**Author:** ![Chintana\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@Chintana\_W](https://www.ruby-forum.com/u/Chintana_W)\
**Post date:** [December 14, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/getting-method-parameters-in-specified-order/122985/9 "2007-12-14T13:54:28Z")

</div>

Chintana W. wrote:

> Hi,
> 
> If I declare a function that expects 3 arguments,
> 
> def bar(x, y, z)  
> …  
> end
> 
> and later, if I have the function name in a string, is there a way to  
> get the parameter names using reflection in Ruby? Something similar  
> to the following PHP snippet,
> 
> $a = new ReflectionFunction(“function\_name”);  
> $a-\>getParameters();

Forgive the naive thoughts of a ruby ruby, but what if you accepted an  
array as the parameter? You could use any number of elements and you  
would always know which is which.
