# Find the parameters of each method in a class

**URL:** <https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345>\
**Category:** Ruby\
**Created:** [January 26, 2010, 7:51pm UTC](https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345 "2010-01-26T19:51:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 26, 2010, 7:51pm UTC](https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345/1 "2010-01-26T19:51:07Z")

</div>

Is there a way to find the parameters of each method in a class? For  
instance I have a class like:

class CoolClass  
def meth\_one(p1,p2)  
…  
end

def meth\_two(p1,p2,p3)  
…  
end  
end

I can run:  
cc = CoolClass.new  
cc.public\_methods(false)

and it will return an array of all the public methods  
([‘meth\_one’,‘meth\_two’]).

Is there a way to find the parameters of each method from the example  
above?

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 26, 2010, 8:10pm UTC](https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345/2 "2010-01-26T20:10:35Z")

</div>

You can find the arity of said methods, but afaik not the names of the  
parameters:

CoolClass.instance\_method(:method\_one).arity # =\> 2  
CoolClass.instance\_method(:method\_two).arity # =\> 3

Jason

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 26, 2010, 9:05pm UTC](https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345/3 "2010-01-26T21:05:41Z")

</div>

Thanks Jason. That gets me a little further. For those that don’t know,  
arity returns an indication of the number of arguments accepted by a  
method.

[http://ruby-doc.org/core/classes/UnboundMethod.html#M001131](http://ruby-doc.org/core/classes/UnboundMethod.html#M001131)

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 26, 2010, 10:30pm UTC](https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345/4 "2010-01-26T22:30:25Z")

</div>

Benoit D. wrote:

> In Ruby 1.9.2:
> 
> irb(main):001:0\> class C  
> irb(main):002:1\> def meth(a,bb, ccc = nil, &b)  
> irb(main):003:2\> end  
> irb(main):004:1\> end  
> =\> nil  
> irb(main):005:0\> C.instance\_method(:meth).parameters  
> =\> [[:req, :a], [:req, :bb], [:opt, :ccc], [:block, :b]]
> 
> But you will not get the names for C methods like String#tr, just the  
> type of the parameters.
> 
> 2010/1/26 Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

Dang thats awesome and exactly what I need! Why can’t it be in 1.8? ☹

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 26, 2010, 11:26pm UTC](https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345/5 "2010-01-26T23:26:53Z")

</div>

On Jan 26, 2010, at 13:30 , Chris G. wrote:

> > But you will not get the names for C methods like String#tr, just the  
> > type of the parameters.
> > 
> > 2010/1/26 Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> Dang thats awesome and exactly what I need! Why can’t it be in 1.8? ☹

It can. Look at merb. I think it is in merb-param-protection. Much more  
hacky, but it works.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 27, 2010, 2:22am UTC](https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345/6 "2010-01-27T02:22:08Z")

</div>

> > irb(main):005:0\> C.instance\_method(:meth).parameters  
> > =\> [[:req, :a], [:req, :bb], [:opt, :ccc], [:block, :b]]
> > 
> > But you will not get the names for C methods like String#tr, just the  
> > type of the parameters.  
> > Dang thats awesome and exactly what I need! Why can’t it be in 1.8? ☹

rdp-arguments gem:

> > require ‘arguments’  
> > class C
> 
> > def meth(a, bb, ccc = nil, &b)  
> > end  
> > end  
> > =\> nil
> 
> > Arguments.names C, :meth  
> > =\> [[:a], [:bb], [:ccc, “nil”]]

You can get it in 1.9.1 with the methopara gem, too.  
GL!  
-r

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [January 26, 2010, 10:21pm UTC](https://www.ruby-forum.com/t/find-the-parameters-of-each-method-in-a-class/182345/7 "2010-01-26T22:21:43Z")

</div>

In Ruby 1.9.2:

irb(main):001:0\> class C  
irb(main):002:1\> def meth(a,bb, ccc = nil, &b)  
irb(main):003:2\> end  
irb(main):004:1\> end  
=\> nil  
irb(main):005:0\> C.instance\_method(:meth).parameters  
=\> [[:req, :a], [:req, :bb], [:opt, :ccc], [:block, :b]]

But you will not get the names for C methods like String#tr, just the  
type of the parameters.

2010/1/26 Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
